这最初是来自另一个程序,但这一部分将不会工作的方式,我需要它,我想其他人可能也有麻烦。还要注意的是,在接受用户输入之后,它将在while循环中使用。
printf("would you like to check another time?(y/n)?");
fflush(stdin);
scanf("% c", &yesno);
while(yesno != 'y' && yesno != 'n')
{
printf("That was not a valid entry, please re entery your choice.");
fflush(stdin);
scanf("% c", &yesno);
}/*End of verification loop*/
我希望用户输入一个字符,在验证它是y或n之后,让它转到while循环,如果字符是y,它将继续程序,如果不是,它将结束它。
最佳答案
printf("would you like to check another time?(y/n)?\n");
fflush(stdin);
scanf("%c", &yesno);
while(yesno != 'n' && yesno != 'y')
{
printf("That was not a valid entry, please re-enter your choice.\n");
fflush(stdin);
scanf("%c", &yesno);
}
if (yesno == 'n') return 0; // program terminated here
// else it is automatically 'y' so your program continues here ...
额外的
我刚刚注意到另一个影响代码片段的关键错误(我还想象了下一行代码)
scanf("% c", &yesno); // will not read input
scanf("%c", &yesno); // will read input, there is no space between % and c, it is %c not % c