I have been working on a program which requires integer inputs, but every time a non-integer is accidentally entered, the scanf function stops working or becomes stuck in a loop.
This is an example of what I tried to do:
int n;
printf("please enter 0 or 1\n");
top:
scanf("%i",&n);
if(n == 0)
{
printf("you entered 0!\n");
}
if(n == 1)
{
printf("you entered 1!\n");
}
if(n != 0 && n != 1)
{
printf("please enter either 0 or 1.");
goto top;
}
The third if is to sends you back to the top if you have not entered the right number, but this doesn't work for letters and other characters. How can you send it back to the top if a non-integer input is given?