小编nAi*_*AiN的帖子

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 …
Run Code Online (Sandbox Code Playgroud)

c debugging stdin scanf

3
推荐指数
1
解决办法
2925
查看次数

标签 统计

c ×1

debugging ×1

scanf ×1

stdin ×1