Before asking this question, I have read: fgetc(stdin) in a loop is producing strange behaviour. I am writing a program to ask the user if they want to exit. Here is my code:
#include<stdio.h>
#include<stdlib.h>
int ask_to_exit()
{
int choice;
while(choice!='y'||choice!='n')
{ printf("Do you want to continue?(y/n):");
while((fgetc(stdin)) != '\n');
choice = fgetc(stdin);
if(choice=='y') return 0;
else if(choice=='n') return 1;
else printf("Invalid choice!\n");
}
}
int main()
{
int exit = 0;
while(!exit)
{
exit = ask_to_exit();
}
return 0;
}
Since fflush(stdin) is undefined behaviour, I did not use it. After following the solution in the other question, I still get the error. Here is a test run of the above program:
$./a.out
Do you want to continue?(y/n):y<pressed enter key>
<pressed enter key>
Invalid choice!
Do you want to continue?(y/n):y<pressed enter key>
<pressed enter key>
Invalid choice!
Do you want to continue?(y/n):n<pressed enter key>
<pressed enter key>
Invalid choice!
Do you want to continue?(y/n):n<pressed enter key>
n<pressed enter key>
<program exits>