Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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()) != '
'){
        if (c == ' ')
            ++nb;
        else if (c == '
')
            ++nl;
        else if (c == '')
            ++nt;
    }
    printf("Input has %d blanks, %d tabs, and %d newlines
", nb, nt, nl);
}

Edit: The 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
718 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


}

Then use Ctrl+Z or F6 on Windows

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

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

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...