As the beginning of my program, I am using a do-while loop to count how many inputs a user enters. The do-while loop should end after EOF (Ctrl+D) is input.
My problem is however, that when I enter 1 value and then press Ctrl+D, my program says it has counted two inputs, when I want it to be only one.
#include <stdio.h>
main()
{
int n=0;
printf("Enter values.\n");
do {
n++;
}
while (EOF!=scanf("%d",&n));
printf("%d\n",n);
}
If I compile the program and run it, and enter a 1, then press enter, and enter Ctrl+D, it says n=2. Why is this? I only entered 1 value.
n++will be executed before Ctrl+D is read. Easiest solution, change to while loop - GoldRoger