c - 忽略无效输入,直到将正确的字符分配给 char 变量

标签 c

在我介绍我的问题之前,我想告诉你我正在使用 Notepad++ 和 Cygwin:

我正在尝试用 C 语言编写一些代码,以便它可以帮助用户在他或她对某些事物的选择之间做出潜在的最佳选择(例如根据用户的喜好决定购买哪台计算机)或提供指导当用户试图提出一些选择来确定潜在的最佳选择时。下面显示的代码不完整,但我首先尝试确保程序只会在用户输入 ynY,或 N:

    #include <stdio.h>

    int  main()
    {
        //This character used for input indicating yes or no.
        char yN = 'a';

        //The following will be used to determine whether the program should help the user decide among certain paths or give the user guidance to do so.
        printf("In the current situation that you have encountered, are you unable to decide between or among a few things? (Y/N)\n");

        //This while loop will ignore invalid input and will "break" once valid input is assigned to the character variable yN
        while(yN != 'y' || yN != 'Y' || yN != 'n' || yN != 'N')
        {
            scanf(" %c", &yN);
        }

        //This will lead to the narrowingPath function.
        if(yN == 'y' || yN == 'Y')
        {
            printf("Good\n");
        }

        //This will lead to the gatheringChoices function.
        else if(yN == 'n' || yN == 'N')
        {
            printf("Hang in there\n");
        }

        //Invalid Input
        else
        {
            printf("Invalid Input\n");
        }

        return 0;
    }

不幸的是,我用于忽略无效输入和寻找有效输入的 while 循环没有按预期工作。

输出和输入尝试:

noahertz@CO1313-25 /cygdrive/x/cpre185/NoahErtz/practice6
$ gcc -o Ertz_Noah_PP_6 Ertz_Noah_PP_6.c -Wall
noahertz@CO1313-25 /cygdrive/x/cpre185/NoahErtz/practice6
$ ./Ertz_Noah_PP_6
In the current situation that you have encountered,are you unable to
decide between or among a few things? (Y/N)
y
Y
n
N
 y
 Y
 n
 N
noahertz@CO1313-25 /cygdrive/x/cpre185/NoahErtz/practice6
$ 

我用 Ctrl+C 取消了程序,因为我什么也得不到。以下是我修改 while 循环时发生的情况:

    //This while loop will ignore invalid input and will "break" once valid input is assigned to the character variable yN
    while(yN != 'y' || yN != 'Y' || yN != 'n' || yN != 'N')
    {
        printf("%c\n", yN);
        scanf(" %c", &yN);
    }

输出和输入尝试:

noahertz@CO1313-25 /cygdrive/x/cpre185/NoahErtz/practice6
$ gcc -o Ertz_Noah_PP_6 Ertz_Noah_PP_6.c -Wall
noahertz@CO1313-25 /cygdrive/x/cpre185/NoahErtz/practice6
$ ./Ertz_Noah_PP_6
In the current situation that you have encountered, are you unable to
decide between or among a few things? (Y/N)
a
y
y
Y
Y
n
n
N
N
 y
y
 n
n
 Y
Y
 N
N
noahertz@CO1313-25 /cygdrive/x/cpre185/NoahErtz/practice6
$ 

因此,如果 yN 按预期分配给 char,为什么程序不退出 while 循环并继续执行 if 和 else 语句?他们工作:

    #include <stdio.h>
    
    int  main()
    {
        //This character used for input indicating yes or no.
        char yN = 'a';
        
        //The following will be used to determine whether the program should help the user decide among certain paths or give the user guidance to do so.
        printf("In the current situation that you have encountered, are you unable to decide between or among a few things? (Y/N)\n");
        scanf(" %c", &yN);

        //This while loop will ignore invalid input and will "break" once valid input is assigned to the character variable yN
        /*while(yN != 'y' || yN != 'Y' || yN != 'n' || yN != 'N')
        {
            printf("%c\n", yN);
            scanf(" %c", &yN);
        }*/

        //This will lead to the narrowingPath function.
        if(yN == 'y' || yN == 'Y')
        {
            printf("Good\n");
        }

        //This will lead to the gatheringChoices function.
        else if(yN == 'n' || yN == 'N')
        {
            printf("Hang in there\n");
        }

        //Invalid Input
        else
        {
            printf("Invalid Input\n");
        }

        return 0;
    }

输出和输入尝试:

noahertz@CO1313-25 /cygdrive/x/cpre185/NoahErtz/practice6
$ gcc -o Ertz_Noah_PP_6 Ertz_Noah_PP_6.c -Wall
noahertz@CO1313-25 /cygdrive/x/cpre185/NoahErtz/practice6
$ ./Ertz_Noah_PP_6
In the current situation that you have encountered, are you unable to
decide between or among a few things? (Y/N)
y
Good
noahertz@CO1313-25 /cygdrive/x/cpre185/NoahErtz/practice6
$ ./Ertz_Noah_PP_6
In the current situation that you have encountered, are you unable to
decide between or among a few things? (Y/N)
Y
Good
noahertz@CO1313-25 /cygdrive/x/cpre185/NoahErtz/practice6
$ ./Ertz_Noah_PP_6
In the current situation that you have encountered, are you unable to
decide between or among a few things? (Y/N)
n
Hang in there
noahertz@CO1313-25 /cygdrive/x/cpre185/NoahErtz/practice6
$ ./Ertz_Noah_PP_6
In the current situation that you have encountered, are you unable to
decide between or among a few things? (Y/N)
N
Hang in there
noahertz@CO1313-25 /cygdrive/x/cpre185/NoahErtz/practice6
$ ./Ertz_Noah_PP_6
In the current situation that you have encountered, are you unable to
decide between or among a few things? (Y/N)
a
Invalid Input
noahertz@CO1313-25 /cygdrive/x/cpre185/NoahErtz/practice6
$ 

如果您需要更多信息来帮助我解决此问题,请告诉我。期待您的回复。

最佳答案

||需要替换成&&如下

while(yN != 'y' && yN != 'Y' && yN != 'n' && yN != 'N')

作为一般规则,每当使用不等运算符 != 将同一变量与多个值进行比较时,两者之间需要有 &&。类似地,每当使用相等运算符 == 将同一个变量与多个值进行比较时,两者之间需要有 ||

关于c - 忽略无效输入,直到将正确的字符分配给 char 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33470323/

相关文章:

c - scanf 第三次无法正常工作

c - 为什么我们在这个 C 程序中使用指针?

c - 将指针传递给具有定义大小的 3D 数组

C 字符串、strlen 和 Valgrind

c - 尝试在终端中运行 C 程序时权限被拒绝

c - 如何将结构重铸为 u32?

c++ - 在 C++ std::vector 和 C 数组之间转换而不复制

C++ 到 C 的转换问题

c - 如何在C中制作分钟和秒计时器

c - 链表 : Should the type of pointer "next" and the name of struct be same?