【发布时间】:2019-11-15 19:14:54
【问题描述】:
int main()
{
int choose, isNum;
printf("Choose 1,2,3,4,5,6\n");
isNum = scanf("%d",&choose);
while(choose != 0){
if(!isNum || choose > 6){
printf("Wrong option!\n");
printf("Choose 1,2,3,4,5,6\n");
isNum = scanf("%d",&choose);
continue;
}
//6 ifs checking which number have you choose and what it does..
printf("Choose 1,2,3,4,5,6\n");
isNum = scanf("%d",&choose);
}}
我从这里尝试了很多东西,flash,cclear,在那一刻,我首先检查它是否是一个数字,然后像许多建议的答案一样使用它,它总是回到同一个无限循环。 我的问题有所不同,因为我尝试了所有答案,正如我所说,它没有帮助,没有 fflush,没有 cclear 并且在使用它之前没有检查它是否是一个数字..
【问题讨论】:
-
在检查
isNum的值之前,您根本不应该使用变量choose。 -
您需要 (a) 清除阻塞读取的输入的输入缓冲区和 (b) 专门检查
isNum != 1(而不是!isNum)否则代码将在 @返回 987654326@。请注意,fflush不应用于输入流。
标签: c scanf infinite-loop