-2

So I'm new to coding in C and coding in general. I'm learning with a book and a code in there just won't work and I don't know why. It's probably a trivial matter but as I said..I'm a bloody noob. Anyway this is the code:

#include <stdio.h>

void main()
{
    char a, b;
    
    printf("Welches Zeichen ist groesser?");
    printf("\nGeben Sie ein Zeichen ein:");
    a = getchar();
    printf("Nun ein anderes Zeichen:");
    fflush(stdin);
    b = getchar();
    
    if( a > b)
    {
        printf("'%c' ist groesser als '%c'!\n", a, b);
    }
    else if( b > a)
    {
        printf("\n'%c' ist groesser als '%c'!\n", b, a);
    }
    else
    {
    printf("\nBitte nicht zweimal das gleiche Zeichen eingeben!");
    }
}

I don't get any compiler error messages, it just seems to 'skip' the second getchar and go straight to the last printf. I feel like it has something to do with fflush(stdin). It doesn't matter if it's in the code or not. I already tried fflush(stdout) but with the same outcome. Can somebody tell me why and please don't be too harsh. Thanks in advance!

  • 3
    The second `getchar()` is reading the newline. – Barmar Sep 03 '20 at 22:52
  • 4
    `fflush(stdin)` is not standard C. You can only flush output streams, not input. – Barmar Sep 03 '20 at 22:52
  • 1
    Does this answer your question? [Using fflush(stdin)](https://stackoverflow.com/questions/2979209/using-fflushstdin) – Nate Eldredge Sep 03 '20 at 22:52
  • 1
    Use `fgets()` to read lines, not `getchar()`. – Barmar Sep 03 '20 at 22:52
  • 1
    If you want to skip or discard something from an input stream, like the newline in this case, the correct solution is to read and ignore it. You can't do it with `fflush`. – Nate Eldredge Sep 03 '20 at 22:53
  • @NateEldredge Thanks, it worked but I have no idea why, honestly. I just replaced the fflush(stdin) with int c; while ( ( c = getchar() ) != EOF && c != '\n' ); what exactly does this mean? – Xelalex Sep 03 '20 at 23:10
  • @Barmar if I use fgets() instead of getchar(), I get 'error c2198: too few arguments for call'. I don't have any problems with getchar() now. – Xelalex Sep 03 '20 at 23:15
  • You need to call it with the correct arguments of course! – Barmar Sep 03 '20 at 23:15
  • It's not a simple replacement for `getchar()`, read the documentation. – Barmar Sep 03 '20 at 23:16
  • See https://stackoverflow.com/questions/12544068/clarification-needed-regarding-getchar-and-newline – Barmar Sep 03 '20 at 23:23
  • One reason why `fflush` is bad for input, is that while it might flush unwanted input from `stdin` before the user continues typing, it would be a disaster when input is redirected from a file. – Weather Vane Sep 03 '20 at 23:25
  • @Xelalex: It continues reading characters, and doing nothing with them, until it finds one that isn't a newline, or until end-of-file. If you learn about the operators involved, it should become clear that this is what it does. And that is indeed exactly what you want - it effectively skips as many newline characters as there may happen to be. – Nate Eldredge Sep 03 '20 at 23:30
  • And if you are reading a book that is recommending the use of `fflush(stdin)` for this, then you should discard that book, as it is likely to contain similar frustrations down the road. – Nate Eldredge Sep 03 '20 at 23:32
  • @NateEldredge I'd have chucked it in the bin by [`void main()`](https://stackoverflow.com/q/204476/1270789) myself. – Ken Y-N Sep 04 '20 at 01:41

1 Answers1

0

As pointed out before fflush() is only for output streams, not input.

To read different lines fgets() might be attractive to you. No fflush() required.

If you can get hold of the ANSI final draft of the early C standard it has two sections, the specification and a "rationale" saying why some choices were made. It really helped me learn C in the days. Now days I download and save an ISO final draft to see where C is now.

7.21.5.2 The fflush function

Synopsis

#include <stdio.h> int fflush(FILE *stream);

Description If stream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined.

If stream is a null pointer, the fflush function performs this flushing action on all streams for which the behavior is defined above

Gilbert
  • 3,740
  • 17
  • 19