count words, getchar debug problem

The following simple program counts the number of words from the stdin (my keyboard). When i press CTR+Z it should stop and present the results. I'm using Windows 10.

If i run the program the ctr+z does NOT work regardless how many times i press it. Could you please help me understand what is happening in the background and the cntr+z does not comply?

I do not want the code that solves the problem, i know that, i just can't figure out
what is happening...and fails. The debugging of this issue i try to understand.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <cstdio>

int main(void)
{
    char ch;
    int wordcnt = 0;

    while ( ( ch = getchar() )!= EOF )
    {
        if(ch != ' ' || ch != '\t' || ch != '\n')
        {
            wordcnt++;
            while( ch != EOF || ch != ' ' || ch != '\t' || ch != '\n')
            {
                ch = getchar(); //<-- the problem is here
                //if(ch == EOF) //<--it solves the problem
                  //  return 0;// <-- it solves the problem
            }
        }
    }
    printf("\nword counter=%d\n",wordcnt);
    return 0;
}


ps: I know that getchar returns EOF if there is nothing to read from the input (line) buffer.
Last edited on
Note that getchar() does not return until you hit <ENTER> or you hit CTRL+Z so this can be confusing the issue. Your program will wait on line 8 until you enter the first line of text and hit <ENTER> Then your program will go into the second loop and process all the characters you just entered until it gets to \n. At that point your program will go back to line 8 and start over. Probably not what you wanted to do. But that is just a side issue.

If you just enter CTRL+Z on the line all by itself then your program should exit. If it does not then I suspect something strange is going on with your IDE tools or OS or some such.
I canceled the solved.
I test the code with 2 printf() . I run it and it falls into a forever loop inside the second while statement. I think the while statement has the condition (ch!=EOF) line 14 ,so it should stop when i press ctr+z. Am i missing something here?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>

int main(void)
{
    char ch;
    int wordcnt = 0;

    while ( ( ch = getchar() )!= EOF )
    {
        if(ch != ' ' || ch != '\t' || ch != '\n')
        {
            wordcnt++;
            printf("1.result: %c\n",ch);
            while( ch != EOF || ch != ' ' || ch != '\t' || ch != '\n')
            {
               printf("\n2.result: %c\n",ch);
                ch = getchar(); //<-- the problem is here
                //if(ch == EOF) //<--it solves the problem
                  //  return 0;// <-- it solves the problem
            }
        }
    }
    printf("\nword counter=%d\n",wordcnt);
    return 0;
}


The ctl+z works fine...no problems with my codeblocks ide, i tested that with a simple program
while( (ch=getchar()!=EOF) );
Last edited on
Topic archived. No new replies allowed.