NAE
NAE

Reputation: 13

Ignore invalid input until the correct character is assigned to a char variable

Before I introduce my problem, I would like to inform you that I am using Notepad++ and Cygwin:

I am trying to write some code in C language so that it will help a user decide a potential top choice between his or her selection of choices for something (like deciding which computer to buy based on the user's preferences) or give guidance while the user tries to come up with some choices to use for determining a potential top choice. The code shown below is incomplete, but I am first trying to make sure that the program will only proceed once a user enters y, n, Y, or 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;
    }

Unfortunately, the while loop that I used for ignoring invalid input and looking for valid input doesn't work as expected.

Output and Input Attempts:

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
$ 

I cancelled out of the program with Ctrl+C because I wasn't getting anywhere. Here's what happens when I modify the while loop:

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

Output and Input Attempts:

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
$ 

So if yN is being assigned the char as intended, why isn't the program exiting out of the while loop and continuing onto the if and else statements? They work:

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

Output and input attempts:

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
$ 

Let me know if you need more information to help me resolve this problem. I look forward to your responses.

Upvotes: 1

Views: 448

Answers (4)

N.Hung
N.Hung

Reputation: 154

I think the problem is that you initialized yN = 'a'. So the condition expression yN != 'y' || yN != 'Y' || yN != 'n' || yN != 'N' is always true.

while(yN != 'y' || yN != 'Y' || yN != 'n' || yN != 'N')
        {
            scanf(" %c", &yN);
        }

Upvotes: 0

user1969104
user1969104

Reputation: 2420

|| needs to be replaced with && as below

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

As a general rule, whenever same variable is compared against multiple values using the inequality operator !=, then there needs to be && in between. Similarly, whenever same variable is compared against multiple values using the equality operator ==, then there needs to be || in between.

Upvotes: 2

bluecliff
bluecliff

Reputation: 67

    while(yN != 'y' || yN != 'Y' || yN != 'n' || yN != 'N')
    {
        scanf(" %c", &yN);
    }

the while condition will always return true, and the code won't step out while loop.

Upvotes: 1

kazinix
kazinix

Reputation: 30123

Shouldn't that be && and not ||?

while(yN != 'y' && yN != 'Y' && yN != 'n' && yN != 'N')
{
    printf("%c\n", yN);
    scanf(" %c", &yN);
}

Upvotes: 2

Related Questions