In the following code, I try to let fflush() to clear the standard input, but when I ran the code, I noticed after the first iteration, it will skip the getchar() part. I read some man page about fflush(). But I couldn't find anything about why fflush() didn't clear the buffer in this case. In first iteration, I typed y after "puts("Prees n to quit"), but in the second iteration getchar() still get y from the buffer.
int main(void)
{
char month_input[size];
typedef struct date_info user_input;
user_input info;
_Bool check = true;
char ch;
while(check)
{
puts("Press n to quit");
printf("ch = %d\n", ch);
if((ch = getchar()) == 'n')
break;
fflush(stdin);
printf("which month?\n");
scanf("%s", month_input);
info.month = month_input;
printf("Enter the date\n");
scanf("%d", &info.date);
printf("Enter the year\n");
scanf("%d", &info.year);
fflush(stdin);
}
printf("month = %s / date = %d / year = %d", info.month, info.date, info.year);
return 0;
}