C中的While循环迭代问题

问题描述

此C代码存在一个错误。在用户输入导致程序进入while循环(错误处理)之后,while循环迭代两次?!

scanf()printf()有关系吗?

输出

Would you like to interact with this program today? (y/n

Please answer the question by typing 'y' or 'n' and hitting enter
Would you like to interact with this program today? (y/n)

Please answer the question by typing 'y' or 'n' and hitting enter
Would you like to interact with this program today? (y/n)

代码

printf("\nWould you like to interact with this program today? (y/n)\n");
char answer; // recording the user's answer in a character
scanf("%c",&answer);

while ((answer != 'y') && (answer != 'n')) { // keeps looping twice???
    printf("\nPlease answer the question by typing 'y' or 'n' and hitting enter\n");
    printf("Would you like to interact with this program today? (y/n)\n");
    scanf("%c",&answer);
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)