【发布时间】:2020-03-13 23:36:22
【问题描述】:
我正在创建一个测试程序,当输入的数字小于或等于十时会显示错误消息:
#include <stdio.h>
void errorMessage()
{
printf("\n This number is less than, or equal to, 10. Please try again. ");
}
int main()
{
int a;
printf("\n Enter a number that is greater than 10. ");
while(scanf(" %d",&a) <= 10)
{
errorMessage();
printf("\n Enter a number that is greater than 10. ");
}
printf("\n This number is greater than 10.");
return 0;
}
当输入小于或等于十的数字(eg 5),然后输入大于十的数字(eg 15)时,问题就来了然后。即使该数字使while 语句为假,程序也会执行errorMessage()。我发现scanf() 以某种方式存储了int a 的输入值。我想知道如何在程序再次运行while 循环之前清除int a 的输入。
【问题讨论】:
-
注意:
" "格式在这里没有用处。
标签: c loops caching while-loop scanf