如何让我的代码免于进入无限循环?
How do I get my code from going into an infinte loop?
我正在重写来自 'C Programming for Absoulute Beginners' 的猜谜游戏代码,以使用 isdigit() 函数验证用户是否输入了数字。
代码的其余部分在错误检查方面有效;但是当用户输入非数字时,代码进入无限循环。
#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);
}
尝试以字符串形式获取输入..您还必须比较 'number' 形式的输入 :)
代码进入无限循环,因为 scanf() 无法读取整数。您输入的字符保留在键盘中 buffer.No 只要该字符存在于缓冲区中,就可以读取更多整数。 scanf() 只是 returns 每次读取为 0 的项目数。因此,程序不会等待用户输入数据和无限循环结果。
scanf() returns 成功读取的项目数。因此,您可以简单地检查 scanf() 的 return 值,如果它是 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);
}
这里不需要要求输入,while循环会继续并在开头提示输入
我正在重写来自 'C Programming for Absoulute Beginners' 的猜谜游戏代码,以使用 isdigit() 函数验证用户是否输入了数字。
代码的其余部分在错误检查方面有效;但是当用户输入非数字时,代码进入无限循环。
#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);
}
尝试以字符串形式获取输入..您还必须比较 'number' 形式的输入 :)
代码进入无限循环,因为 scanf() 无法读取整数。您输入的字符保留在键盘中 buffer.No 只要该字符存在于缓冲区中,就可以读取更多整数。 scanf() 只是 returns 每次读取为 0 的项目数。因此,程序不会等待用户输入数据和无限循环结果。
scanf() returns 成功读取的项目数。因此,您可以简单地检查 scanf() 的 return 值,如果它是 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);
}
这里不需要要求输入,while循环会继续并在开头提示输入