I made this code while I was practicing C language. But apparently this code does not rewind the input. So when I enter some data type different than int
, it is upposed to go back to beginning of the while
loop and start the question again. But instead of doing that, it just prints stuff infinitely. Seems like it does not rewind what`s in the buffer. I was wondering why it does that. And I use online compiler because my environment does not allow downloading Visual Studio or any compiler.
void main()
{
char account[64];
char password[64];
int i, rAns;
while (1)
{
printf("1.already a member? Log in\n");
printf("2.register\n");
if (scanf("%d", &rAns) == 0)
{
printf("enter right answer\n");
rewind(stdin);
}
else
{
if (rAns < 1 || rAns > 2)
{
printf("enter one of the options\n");
}
else
{
break;
}
}
}
}