I need to check if the user entered a integer in a scanf() (in C). I have this code:
do {
srs = scanf("%d", &x);
} while (!srs);
If I enter a number it works and continues with the program, but if I enter a char, it just asks me for input again. Though, if then of this I enter a correct integer, it don’t break the loop.
When you don’t enter the number first, it leaves the non-numeric character on the input stream for the next call to pick up. So you get in an infinite loop.
You should be using
fgetsto read a whole line of input, thensscanfon that input line.