while ((c = getchar()) != EOF) Not terminating

I've been reading "The C Programming Language" and I got to this part of inputs and outputs.

I've read other threads saying that the console doesn't recognize enter as EOF. So that I should use CTRL + Z in Windows or CTRL + D in Unix (neither of those is working for me).

I also read other people asking the same saying they could make it work, the problem in their codes was syntax not the program not terminating.

Is there another solution?

This is the code:

#include <stdio.h>
main()
{
    int nb, nl, nt, c;
    nb = 0;
    nl = 0;
    nt = 0;
    while ((c = getchar()) != '\n'){
        if (c == ' ')
            ++nb;
        else if (c == '\n')
            ++nl;
        else if (c == '\t')
            ++nt;
    }
    printf("Input has %d blanks, %d tabs, and %d newlines\n", nb, nt, nl);
}

Edit: The \n was supposed to be an EOF, I was messing around before I posted and I forgot I changed it :P

It doesn't work with EOF neither, I just skipped that one.


Solution 1:

while ((c = getchar())  !=EOF) {


}

Then use Ctrl+Z or F6 on Windows

Following will wait for either a \n or EOF, which comes first

while((c = getchar()) != '\n' && c != EOF){

}

Solution 2:

On Windows, you type Ctrl-Z on a line by itself (no spaces or anything) and type the Return after that. On Windows, you could technically skip the EOF indicator and keep reading characters, though this doesn't apply to other operating systems because EOF actually means EOF.

Solution 3:

while ((c = getchar()) != '\n'){
    if (c == ' ')
        ++nb;
    else if (c == '\n')
        ++nl;
    else if (c == '\t')
        ++nt;
} 

According to the condition in your while loop, you will not be able to count number of new lines because you will stop the while loop when the user inputs a new line character ('\n')

Other than that, counting blanks and tabs just works fine.

CTRL+Z will be recognized as a EOF in windows. But in order to recognize it in your program, use condition in while loop as ((c = getchar()) != EOF). Now when the user presses the key combination: CTRL+Z, It will input to console as EOF, and the program should recognize it as a character input.

Doing this will allow you to count number of lines in the input

So, my suggestion is:

while ((c = getchar()) != EOF){
    if (c == ' ')
        ++nb;
    else if (c == '\n')
        ++nl;
    else if (c == '\t')
        ++nt;
} 

Solution 4:

If you are on unix system:

The only way to simulate EOF via keyboard while feeding input to the program manually is to press CTRL+D

Here are a couple methods of feeding input to your program and be able to signal EOF at the end of the input:

  • Make use of the here string format to feed a string representation of a file to the program.
./myprog <<< "Here is a string to work with"
  • Alternatively, you can also use input redirection to feed a file containing the input to the program.
./myprog < input.file

Any of the above listed methods will work with the following code:

#include <stdio.h>
#ifndef EOF
#define EOF (-1)
#endif
int main(void)
{
    int nb, nl, nt, c;
    nb = 0;
    nl = 0;
    nt = 0;
    while ((c = getchar()) != EOF){
        if (c == ' ')
            ++nb;
        else if (c == '\n')
            ++nl;
        else if (c == '\t')
            ++nt;
    }
    printf("Input has %d blanks, %d tabs, and %d newlines\n", nb, nt, nl);
    return 0;
}
  • Not to leave windows out of the game. To simulate EOF on keyboard on windows, use CTRL+Z key combination

Not sure if the here-string format for unix is available for windows, but input redirection should be similar