如何让我的代码进入infinte循环?

时间:2022-04-30 20:07:07

I am rewriting the Guessing Game code from 'C Programming for Absoulute Beginners' to verify that the user has entered in a digit, using the isdigit() function.

我正在使用isdigit()函数重写“C编程为Absoulute初学者”中的猜测游戏代码,以验证用户是否输入了数字。

The rest of the code works, in terms of error checking; but the moment that the user enters in a non-digit, the code goes into an infinite loop.

其余代码在错误检查方面有效;但是当用户输入非数字时,代码进入无限循环。

#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


4  

The code goes into infinite loop as scanf() is unable to read an integer. The character you entered remains in the keyboard buffer.No more reading of integers is possible as long as the character is present in the buffer. scanf() simply returns the number of items read as 0 each time. Hence,the program does not wait for the user to enter data and infinite loop results.

由于scanf()无法读取整数,因此代码进入无限循环。您输入的字符仍保留在键盘缓冲区中。只要字符在缓冲区中,就不可能读取整数。 scanf()只返回每次读为0的项目数。因此,程序不会等待用户输入数据和无限循环结果。

scanf() returns number of items successfully read. So,you can simply check for the return value of scanf(), if its 1 then scanf() has correctly read an integer.

scanf()返回成功读取的项目数。因此,您可以简单地检查scanf()的返回值,如果其为1,则scanf()已正确读取整数。

   check = scanf("%d", &iResponse);

   if(check == 1){
        printf("\nThank you\n");
        printf("\nYou entered %d\n", iResponse);

and flush the buffer if wrong input is entered

如果输入错误输入,则刷新缓冲区

        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);
        }

no need to ask for input here, while loop will continue and prompt for input in the beginning

这里不需要输入,while循环将继续并在开头提示输入

#2


1  

Trying taking the input in the form of string .. also u will have to compare the input in the form 'number' :)

尝试以字符串的形式获取输入..你也必须比较“数字”形式的输入:)

#1


4  

The code goes into infinite loop as scanf() is unable to read an integer. The character you entered remains in the keyboard buffer.No more reading of integers is possible as long as the character is present in the buffer. scanf() simply returns the number of items read as 0 each time. Hence,the program does not wait for the user to enter data and infinite loop results.

由于scanf()无法读取整数,因此代码进入无限循环。您输入的字符仍保留在键盘缓冲区中。只要字符在缓冲区中,就不可能读取整数。 scanf()只返回每次读为0的项目数。因此,程序不会等待用户输入数据和无限循环结果。

scanf() returns number of items successfully read. So,you can simply check for the return value of scanf(), if its 1 then scanf() has correctly read an integer.

scanf()返回成功读取的项目数。因此,您可以简单地检查scanf()的返回值,如果其为1,则scanf()已正确读取整数。

   check = scanf("%d", &iResponse);

   if(check == 1){
        printf("\nThank you\n");
        printf("\nYou entered %d\n", iResponse);

and flush the buffer if wrong input is entered

如果输入错误输入,则刷新缓冲区

        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);
        }

no need to ask for input here, while loop will continue and prompt for input in the beginning

这里不需要输入,while循环将继续并在开头提示输入

#2


1  

Trying taking the input in the form of string .. also u will have to compare the input in the form 'number' :)

尝试以字符串的形式获取输入..你也必须比较“数字”形式的输入:)