Char variables as an input to a Y/N question

Viewed 355

I'm asking the user if he/she wants a drink. The only answer the user can type is Y for yes and N for no then there comes the final dialogue. I made the answer a char: char answer; then added an if statement. If the user types Y or N, everything's fine. However, whenever the user types Y(+ any letter) such as YHTY, the program still accepts it (same goes for N). I just need a single letter answer Y or N and other answer beyond that would lead to the else dialogue.

This is the code:

    #include <iostream>

    using namespace std;

    int main()
    {
        char answer;

        cout << "Do you want a drink? " << endl;
        cout << "Y/N: ";
        cin >> answer;

        if(answer == 'Y' || answer == 'y'){
            cout << "Okay. I'll bring you some." << endl;
        }   else if(answer == 'N' || answer == 'n'){
            cout << "Okay suit yourself." << endl;
        }   else{
            cout << "Please type just Y/N." << endl;
        }
        return 0;
    }
2 Answers

Since you want to deal with more than one character you have to make your variable a string. For instance

#include <string>

    string answer;

    cout << "Do you want a drink? " << endl;
    cout << "Y/N: ";
    getline(cin, answer);
    if (answer == "Y" || answer == "y") {

getline reads a single line of text, and puts it in a string.

Although this does exactly what you said it probably doesn't do exactly what you want. Suppose the user types the spacebar and then Y, I would expect that to be treated exactly the same as if they'd just typed Y, but the above code will reject any input with spaces. Input validation is always harder than you think, and as a beginner not really worth wasting your time on (IMHO).

cin will accept only the first letter of input as answer as the data type of it is char, I don't see any problem here, it should server your need. You can check above statement through below code:

    #include <iostream>

    using namespace std;

    int main()
    {
        char answer,a2;

        cout << "Do you want a drink? " << endl;
        cout << "Y/N: ";
        cin >> answer;
        cin>>a2;
        cout<<endl<<a2<<endl;

        if(answer == 'Y' || answer == 'y'){
            cout << "Okay. I'll bring you some." << endl;
        }   else if(answer == 'N' || answer == 'n'){
            cout << "Okay suit yourself." << endl;
        }   else{
            cout << "Please type just Y/N." << endl;
        }
        return 0;
    }

a2 will hold the second character in input.


But if you really want to validate only a single character then you can change data type of answer to char[] or string then you can validate the length of input first then check value.

    #include <iostream>

    using namespace std;

    int main()
    {
        char str[3], answer;

        cout << "Do you want a drink? " << endl;
        cout << "Y/N: ";
        cin >> str;
        answer=str[0];

        if(str[1]!='\0'){ // Checks if second char is not NULL char i.e. str length is not 1
            cout << "Please enter one character only." << endl;
        }   else if(answer == 'Y' || answer == 'y'){
            cout << "Okay. I'll bring you some." << endl;
        }   else if(answer == 'N' || answer == 'n'){
            cout << "Okay suit yourself." << endl;
        }   else{
            cout << "Please type just Y/N." << endl;
        }
        return 0;
    }
Related