0

I have just started learning C, been reading a C textbook by Keringhan and Ritchie. There was this example in the textbook, counting characters from user input. Here's the code:

#include <stdio.h>

main() 
{
    long nc;
    nc = 0;

    while(getchar() != EOF) {
        if (getchar() != 'q')       
            ++nc;
        else 
            break;      
    }

    printf("%ld\n", nc);        
}

The problem is, when I execute the code, if I input only one character per line, when I input "q" to break, it doesn't do so. I have to type some word per line, only after that it will break the loop. Also, it only counts the half of the characters of the word. I.e. if I input

a
b
russia

it will only print '5' as final result.

Could you please explain to me why is this happening?

Benoît Guédas
  • 801
  • 7
  • 25
kulan
  • 1,353
  • 3
  • 15
  • 29
  • 2
    The above code is odd. You should store the while getchar() as a variable and then check if the variable is == 'q' instead of calling getchar() again. – David Mar 22 '14 at 12:01
  • It use twice the `getchar`. – BLUEPIXY Mar 22 '14 at 12:02
  • In other words, your code is ignoring every other `getchar()` call (where the comparison to 'q' and counting characters is concerned). This makes you susceptible to not seeing the `q`, and it makes your count divide the true number of key presses by 2. – mah Mar 22 '14 at 12:10
  • I used a variable instead of using getchar() the second time, but now I think it counts Enter as well. Is it supposed to be like that. – kulan Mar 22 '14 at 14:29
  • I'm sure that K&R doesn't contain such broken code, unless you got one of the (sometimes atrocious) translations in your hand. – vonbrand Mar 22 '14 at 15:00
  • It didn't actually. I added IF statements myself, because otherwise I would know how to exit the loop. – kulan Mar 22 '14 at 15:32

1 Answers1

1

This works, but only when you finish off with an Enter. So, this will count the characters until the first "q" appears. That is just how getchar() and getc(stdin) work.

#include <stdio.h>

int main() {
    char c = 0;
    long count = 0;
    short int count_linebreak = 1; // or 0

    while((c = getchar()) != EOF) {
        if(c != 'q' && (count_linebreak || (!count_linebreak && c != '\n'))) {
            ++count;
        }else if(c == 'q') {
            printf("Quit\n");
            break;
        }
    }

    printf("Count: %ld\n",count);

    return 0;
}

A StackOverflow question about reading stdin before enter C read stdin buffer before it is submit

Community
  • 1
  • 1
David
  • 4,313
  • 1
  • 21
  • 29
  • So when you press Enter, it will aslo be counted? Becuase it does in my code. E.g. I input 3 chars in 3 lines, and I get Count = 6. – kulan Mar 22 '14 at 14:27
  • Line Breaks '\n' are characters and will be counted. I will update the code to not count line breaks also. The enter statement from above means that if you type q and don't press enter, nothing will happen. – David Mar 22 '14 at 14:43
  • not quite got your code with count_linebreak, but when it is set to 1, same problem, it counts Enter as well. And when it is set to 0, the program terminates after the first Enter. – kulan Mar 22 '14 at 15:31
  • It shouldn't end after the first enter. Make sure you update the else statement so it checks if c == 'q', before it was just an else. When set to 1, it should count line breaks, when set to 0 it shouldn't. – David Mar 22 '14 at 15:38