I try to make a C console program ask user enter Y (means yes) or N (means no) by using if
and while
,the user only can enter Y or N,otherwise the program will tell the user input error,no matter they input what the characters, how many strings.The fuction just like terminal ask user “Are you sure?Input Y or N".
But my program will give me more than one error feedback when i test.I relly want it just give me one feedback.Hope you can help me perfect my progrem.
Here are my code:
#include <stdio.h>
int main(void)
{
char yon = '\0';
do
{
if (yon != '\0')
printf("\nSorry,Please try again.\n");
printf("\nEnter Y/N?:");
}
while ((yon = getchar()) != 'y'&&yon != 'Y'&&yon != 'n'&&yon != 'N');
return 0;
}
My English is not good,If you do not know what i mean,Please tell me to edit.
Thanks.