0
int main()
{
    int choose, isNum;
    printf("Choose 1,2,3,4,5,6\n");
    isNum = scanf("%d",&choose);
    while(choose != 0){
        if(!isNum || choose > 6){
            printf("Wrong option!\n");
            printf("Choose 1,2,3,4,5,6\n");
            isNum = scanf("%d",&choose);
            continue;
        }

        //6 ifs checking which number have you choose and what it does..

        printf("Choose 1,2,3,4,5,6\n");
        isNum = scanf("%d",&choose);
}}

I tried many things from here, fflash, cclear, and at that moment I'm checking first if its a number then using it like many of the answers suggested and it always getting back to the same infinite loop. my question is different because I tried every answers and as I said, it didn't help, not fflush, not cclear and not checking if its a number before using it..

Asaf
  • 7
  • 1
  • You should not use the variable `choose` at all before you have checked the value of `isNum`. – klutt Nov 15 '19 at 19:23
  • You need to (a) clear the input buffer of the input that is blocking the read and (b) check specifically for `isNum != 1` (instead of `!isNum`) otherwise the code will fail when `EOF` is returned. Note that `fflush` should not be used for input stream. – Weather Vane Nov 15 '19 at 19:24

1 Answers1

0

That's how scanf() works with invalid input. I see that you check with isNum if proper input has been given but that choose in the condition is not correct. The thing is that what have you written stays in the buffer if not consumed from scanf(), that's what causing the infinite loop. You must write code so that the buffer is cleared, and then invoke scanf() again.

One solution for clearing the buffer is this piece of code: while ((getchar()) != '\n');

NickDelta
  • 3,697
  • 3
  • 15
  • 25
  • Do you mean to use the fflush(stdout); function? because I already tried it, I entered it before every scanf and it still enter the same loop. – Asaf Nov 15 '19 at 19:31
  • @Asaf stdout is not the stream you must clear. It is stdin . But instead of flushing you can write this : while ((getchar()) != '\n'); and it should clear the buffer for you. It consumes whatever has remained in the buffer – NickDelta Nov 15 '19 at 19:34
  • thanks, fflush(stdin) works, its a school thing so I can't use getchar function but how is it different than while(choose != '\n') because that didn't work and also, why is it matter not using the fflush? – Asaf Nov 15 '19 at 19:46
  • @Asaf See [Using fflush(stdin)](https://stackoverflow.com/questions/2979209/using-fflushstdin) – chux - Reinstate Monica Nov 15 '19 at 19:53
  • It is undefined behavior to use fflush(stdin). In some compilers and systems may work in some other may not. Please consider marking my answer as the accepted one. – NickDelta Nov 15 '19 at 19:53
  • @Asaf If you're not allowed to call `getchar` to fetch and discard the "bad" characters, then just make sure you don't type "bad" characters in the first place. Please don't look for an excuse to call `fflush(stdin)` -- it really is a bad thing to do. The bottom line is that `scanf` isn't a very good input function, and one of the things it's poorest at is dealing with malformed input. If you want to write a program that deals well with malformed input, the first thing to do is not use `scanf` at all. (And then the urge to call `fflush` doesn't even come up.) – Steve Summit Nov 15 '19 at 19:59