scanf 无法处理无效输入

nAi*_*AiN 3 c debugging stdin scanf

在第一个字符输入时scanf(),第二个不运行。getchar()对于 Try Again 输入也不起作用。它跳过输入 你想再玩一次吗?(是/否)?似乎your_choice应该拿下这个角色然后再检查一下,但实际上这个角色被ch. 是什么导致它像这样工作以及如何解决这个问题。我试过重新初始化变量但不起作用。

#include <stdio.h>

void choice(int);

int main() {
    char ch;
    int random, your_choice;

    do {
        srand(time(NULL));
        system("cls");
        printf("** 0 is for Rock **\n");
        printf("** 1 is for Scissors **\n");
        printf("** 2 is for Lizard **\n");
        printf("** 3 is for Paper **\n");
        printf("** 4 is for Spock **\n");

        printf("\nEnter your choice here:");
        scanf("%d", &your_choice);

        random = rand() % 5; //random number between 0 & 4
        if ((your_choice >= 0) && (your_choice <= 4)) {
            //choice printer omitted for this post

            if ((random == ((your_choice + 1) % 5)) || (random == ((your_choice + 2) % 5)))
                printf("\n\n... and you win!!!\n");
            else if ((random == ((your_choice + 3) % 5)) || (random == ((your_choice + 4) % 5)))
                printf("\n\n... and you lose!!!\n");
            else if (random == your_choice)
                printf("\n\nUnfortunately, it's a tie!\n");
        } else
            printf("\nWell, this is wrong! Try again with a number from 0 to 4!!\n");

        printf("\nWould you like to play again? (Y/N)?: ");
        scanf(" %c", &ch);

    } while (ch == 'y' || ch == 'Y');

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

chq*_*lie 5

如果用户输入无法转换为数字的字符,则scanf("%d", &your_choice);返回 0 且your_choice保持不变,因此未初始化。行为未定义。

您应该对此进行测试并以这种方式跳过有问题的输入:

    if (scanf("%d", &your_choice) != 1) {
        int c;
        /* read and ignore the rest of the line */
        while ((c = getchar()) != EOF && c != '\n')
            continue;
        if (c == EOF) {
            /* premature end of file */
            return 1;
        }
        your_choice = -1;
    }
Run Code Online (Sandbox Code Playgroud)

解释:

  • scanf()返回成功转换的次数。如果用户输入一个数字,它被转换并存储为your_choicescanf()返回 1,如果用户输入了一些不是数字的东西,例如AAscanf()将有问题的输入留在标准输入缓冲区中并返回 0,最后如果文件结束到达(用户在 windows 中键入 ^Z enter 或在 unix 中键入 ^D),scanf()返回EOF.

  • 如果输入未转换为数字,我们将输入if语句的主体:使用 , 一次消耗一个字节的输入getchar(),直到读取文件末尾或换行。

  • 如果getchar()返回EOF,我们已经读取了整个输入流,不需要提示用户输入更多信息,您可能希望在返回错误代码之前输出错误消息。

  • 否则,设置your_choice-1一个无效值,以便读取代码时会报错并提示进一步输入。

读取和丢弃有问题的输入是必要的:如果您不这样做,下一个输入语句scanf(" %c", &ch);将读取有问题的输入的第一个字符,而不是等待用户输入以响应Would you like to play again? (Y/N)?:提示。这是对您观察到的行为的解释。