C 如何从进入infinte循环中获取代码?
我正在重写“Absolute初学者C编程”中的猜谜游戏代码,以验证用户是否使用isdigit()函数输入了数字 代码的其余部分在错误检查方面起作用;但一旦用户输入非数字,代码就会进入无限循环C 如何从进入infinte循环中获取代码?,c,loops,infinite,C,Loops,Infinite,我正在重写“Absolute初学者C编程”中的猜谜游戏代码,以验证用户是否使用isdigit()函数输入了数字 代码的其余部分在错误检查方面起作用;但一旦用户输入非数字,代码就会进入无限循环 #include <stdio.h> #include <stdlib.h> #define NO 2 #define YES 1 main() { int guessGame; guessGame = 0; int iRandomNum =
#include <stdio.h>
#include <stdlib.h>
#define NO 2
#define YES 1
main()
{
int guessGame;
guessGame = 0;
int iRandomNum = 0;
int iResponse = 0;
printf("\n\nWould you like to play \"The Guessing Game\"?\n\n");
printf("\nType '1' for Yes or '2' for No!\n\n");
scanf("%d", &guessGame);
do{
if(guessGame == YES){
iRandomNum = (rand() % 10) + 1;
printf("\nGuess a number between 1 and 10:\n\n ");
scanf("%d", &iResponse);
if(!isdigit(iResponse)){
printf("\nThank you\n");
printf("\nYou entered %d\n", iResponse);
if(iResponse == iRandomNum){
printf("\nYou guessed right\n");
printf("\nThe correct guess is %d!\n", iRandomNum);
printf("\nDo you wish to continue? \n");
printf("\nType '1' for Yes or '2' for No!\n\n");
scanf("%d", &guessGame);
} else {
printf("\n\nSorry, you guessed wrong\n");
printf("\nThe correct guess was %d!\n", iRandomNum);
printf("\n\nDo you wish to continue? \n");
printf("\nType '1' for Yes or '2' for No!\n\n");
scanf("%d", &guessGame);
}
}
else {
printf("\nYou did not enter a digit\n");
printf("\n\nPlease enter a number between 1 and 10:\n\n");
scanf("%d", &iResponse);
}
}
else {
printf("\nThe window will now close. Try again later!\n");
exit(0);
}
}while(guessGame != NO);
}
#包括
#包括
#定义2
#定义是1
main()
{
智力猜谜游戏;
猜游戏=0;
int iRandomNum=0;
int i响应=0;
printf(“\n\n您想玩猜游戏吗?”\n\n”);
printf(“\n键入“1”表示是,键入“2”表示否!\n\n”);
scanf(“%d”和猜游戏);
做{
如果(猜游戏==是){
iRandomNum=(rand()%10)+1;
printf(“\n请输入一个介于1和10之间的数字:\n\n”);
scanf(“%d”和&i响应);
如果(!isdigit(i响应)){
printf(“\n谢谢您\n”);
printf(“\n您输入了%d\n”,i响应);
如果(iResponse==iRandomNum){
printf(“\n您猜对了\n”);
printf(“\n正确的猜测是%d!\n”,iRandomNum);
printf(“\n要继续吗?\n”);
printf(“\n键入“1”表示是,键入“2”表示否!\n\n”);
scanf(“%d”和猜游戏);
}否则{
printf(“\n\n您猜错了”\n”);
printf(“\n正确的猜测是%d!\n”,iRandomNum);
printf(“\n\n要继续吗?\n”);
printf(“\n键入“1”表示是,键入“2”表示否!\n\n”);
scanf(“%d”和猜游戏);
}
}
否则{
printf(“\n您没有输入数字\n”);
printf(“\n\n请输入一个介于1和10之间的数字:\n\n”);
scanf(“%d”和&i响应);
}
}
否则{
printf(“\n窗口现在将关闭。请稍后再试!\n”);
出口(0);
}
}while(猜游戏!=否);
}
尝试以字符串的形式进行输入。。此外,u还必须比较形式为“number”:)的输入。由于scanf()无法读取整数,代码进入无限循环。输入的字符保留在键盘缓冲区中。只要该字符存在于缓冲区中,就不可能再读取整数。scanf()只返回每次读取为0的项数。因此,程序不会等待用户输入数据和无限循环结果
scanf()返回成功读取的项目数。因此,您只需检查scanf()的返回值,如果其值为1,则scanf()已正确读取整数
check = scanf("%d", &iResponse);
if(check == 1){
printf("\nThank you\n");
printf("\nYou entered %d\n", iResponse);
如果输入错误,则刷新缓冲区
else {
while (getchar() != '\n'); //flush the buffer
printf("\nYou did not enter a digit\n");
printf("\n\nPlease enter a number between 1 and 10:\n\n");
//scanf("%d", &iResponse);
}
无需在此处请求输入,而循环将继续并在开始时提示输入您使用了什么调试器来逐步完成此操作?如果(!isdigit(iResponse))不应该是
如果(isdigit(iResponse))
?如果它确实在检查一个数字..@Ben,我还没有使用任何调试器来逐步调试代码。我正在使用DevC++编写和编译它。但是,不幸的是,调试器不起作用,我也没有时间去寻找解决方案使它起作用。有什么想法吗?@David.C.Rankin,真的那么简单吗?@downshift我也这么想。但是,当我使用if(isdigit(iResponse))时,相应的代码块被跳过,这不是我想要的。谢谢你的评论@Hardik。它当然涉及我的无限循环问题,这是非常感谢。但是,关于您关于scanf()无法读取整数的评论,您能告诉我为什么这个问题没有在外部循环中发生:scanf(“%d”,&guessGame);?此外,是否有方法使用isdigit()函数完成相同的任务?您已经初始化了guessGame=0;因此,对于外部循环,当输入无效输入时,scanf()将无法读取。因此猜游戏仍然是0。然后在循环内部,检查是否存在(guessGame==YES),它是否会从循环中出来并停止执行。使用调试器,您可以使用它理解/解决所有问题。非常感谢@Hardik的澄清。非常感谢。我的意图是使用调试器。然而,我新安装的DevC++似乎没有启用它。关于如何设置它有什么想法吗?