> while ((c = getchar()) != EOF)
Unfortunately, EOF is a sticky state, so attempting to do
while( (c = getchar()) != '\n' );
simply doesn't do what you want it to when you've just pressed ctrl-z to exit your first while loop.
1. The easy thing to try is to open a console prompt and run the program from there, using say
cd c:\code
myprog.exe
All of the traditional examples (including those in K&R) assume you're working at a command prompt interface.
2. Use the external pause program to do the wait
Code:
int main ( ) {
// your code here
system("PAUSE");
return 0;
}
It's not ideal, but gets past the EOF problem.
3. Fix the EOF problem inside your code.
Code:
int main ( ) {
int c, i, nwhite, nother;
int ndigit[10];
nwhite = nother = 0;
for (i = 0; i < 10; ++i)
ndigit[i] = 0;
while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c-'0'];
else if (c == ' ' || c == 'n' || c == '\t')
++nwhite;
else
++nother;
printf("digits =");
for (i = 0; i < 10; ++i)
printf(" %d", ndigit[i]);
printf(", white space = %d, other = %d\n",
nwhite, nother);
printf ("Press [Enter] to continue");
clearerr( stdin );
while ((c = getchar()) != '\n' && c != EOF);
return 0;
}