-
Incrementing Twice?
Hi! So. I am trying to write a program that counts the number of characters entered into the program from the keyboard until EOF. My teacher is having me use the getchar() function. The problem is my program seems to want to increment the count variable twice every time...
Code:
#include <stdio.h>
int main( int argc, char *argv[] ) {
int c;
int count;
count = 0;
while ( ( c = getchar() ) != EOF ){
count ++;
}
printf ("\n%d characters were entered.\n\n", count);
return 0 ;
}
So if I entered 3 4 and 5, it would report 6. Does anyone have any ideas why this is happening? Did I just make a really stupid mistake? Thanks in advance.
-
Because you entered six characters:
3
enter-key
4
enter-key
5
enter-key
That's six characters. (Maybe the first two were spaces, but the point still holds.)
-
As in you entered 345, then simulated EOF and it reported 6? If you entered 3, then entered 4, then entered 5, the newline characters due to the enters will be counted.
-