[Solved] C- number checking function gives infinite loop [closed]


Note the line below with the comment about eating the input buffer. Since your scanf didn’t find what it is looking for in the input buffer, the wrong input just stays there and fails forever unless you do something to “eat” it.

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    printf("Hello world!\n");
    while ('A')
    {
        int x, y;
        printf("\n x: ");

        y = scanf ("%d", &x);
        printf("\nx = %d", x);
        if (y < 1)
        { // eat the input buffer so we can try again
            while ( getchar() != '\n' );
            printf ("\nWRONG!");
        }
    }

    return 0;
}

1

solved C- number checking function gives infinite loop [closed]