【发布时间】:2018-03-11 21:21:55
【问题描述】:
我很确定之前有人问过这个问题,但我找不到任何答案,所以我开始了。这是一行简单的代码,我无法让它退出while loop。如果我将|| 更改为&&,无论我按什么,循环都会退出。谢谢你的回答。
#include <stdio.h>
int main()
{
int answer;
printf("Are you sure you want to exit the program? Type in 1 for yes and 2 for no.\n");
scanf("%d", answer);
//This is to check that the user inputs the right number if not error message is displayed
while(answer <1 || answer > 2)
{
printf("Please type in 1 to exit the program and yes and 0 to keep playing. \n");
scanf("%d", answer);
flushall();
}
return 0;
}
【问题讨论】:
-
减少你的问题。此外,如果没有 MCVE(请参阅网站指南),您的问题将是题外话。
-
请记住,您需要将
&answer而不是answer作为参数传递给scanf。 :-)
标签: c while-loop scanf