[Solved] Stuck in infinite fgets loop


If there are more than one statements that you want to write in a loop or if statement, then you must use braces {} to define which statements come in the loop.

In your code, only the printf("Insert a non negative number: "); statement is in the loop, and nothing is changing the input‘s value in the loop, so if the loop’s condition is true, it will always remain true.

Change it to:

while ( atoi(input) < 0 || ( strcmp(input, "0") != 0 && atoi(input) == 0 ) )
{
    printf("Insert a non negative number: ");
    fgets(input, 4, stdin);
}

0

solved Stuck in infinite fgets loop