简体   繁体   中英

I need help on this C++ yes/no problem while using logical operators

So the problem is: Write a program that prints the question "Do you wish to continue?" and reads the input. If the user input is "Y", "Yes", "YES", then print out "Continuing". If the user input is "N" or "No", "NO" then print out "Quit". Otherwise, print "Bad Input". Use logical operators.

So far this is all the code that I have written. I know that it is not complete, and I do not know what else I need to add to the code.

#include <iostream>

using namespace std;

int main() {

    char response;

    cout << "Do you wish to continue?" ;
    cin >> response;

    if (response == 'Y'){
        cout << "Continuing";
    }

    else if (response == 'N'){
        cout << "Quit";
    }
    else if (response != 'N' || 'Y'){
        cout << "Bad input";
    }

    return 0;
}

Update: so I edited my code and it is still giving me a bunch of errors. It's making me frustrated lol. Keep in mind I'm a beginner and we haven't learned loops yet. Sorry for the headache!

#include <iostream>
#include <string>
using namespace std;

int main() {

    char response;
    string help;



    cout << "Do you wish to continue?" ;
    cin >> response, help;


    if (response == 'Y' || help == "Yes" || help == "YES"){

        cout << "Continuing";

    }

    else if (response == 'N' || help == "No" || help == "NO"){
        cout << "Quit";
    }
    else if (response != 'N' || response != 'Y' || help != "Yes" || help != "YES" || help != "No" || help != "NO"){
        cout << "Bad input";
    }

    return 0;
}

First off I think this is a great start. Sounds like you are new to C++ so here are some suggestions:

1) Your response variable can only contain a character. I would suggest including string and changing the response to take a string from the user for 'Y', "Yes", etc.

2) I suggest wrapping your code in a while loop with an exit condition.

3) Each of your logic branches should include a return integer. This will give the program an exit condition if the logical conditions are met.

I know I haven't given you the answers fully. If you are truly stuck, reply back and we can walk through.

A simple way is to simply convert the user's answer to uppercase or lowercase. By doing this, you can simply use the lower case. For your loop, you could for example use a "do..while".

#include <iostream>
#include <string>
using namespace std;

int main() {
    int stop = 0;
    string response;

    //Continue until the user choose to stop.
    do{
        //-------------
        // Execute your program
        //-------------
        cout << "Do you wish to continue? ";
        cin >> response;

        //-------------
        //Convert to lower case
        for (string::size_type i=0; i < response.length(); ++i){
            response[i] = tolower(response[i]);
        }
        //-------------
        //Check the answer of the user.
        if (response.compare("y") == 0 || response.compare("yes") == 0){
            cout << "Continuing \n";
        }
        else if (response.compare("n") == 0 || response.compare("no") == 0){
            cout << "Quit \n";
            stop = 1;
        }
        else{
            cout << "Bad input \n";
        }

    }while(stop == 0);

    return 0;
}

Like you said in the question, we care about Y,Yes,YES,N,No and NO. For anything else we need to print "Bad Input". Think about how you'd be storing these responses (hint: Sam Varshavchik's answer).

Once you've taken care of extracting user input, you'd want to check what the user actually entered and proceed accordingly. From your question it seems "if else" would do. You need to change the conditionals for your "if else ifs" because you have 3 conditions for one type of response: Y, Yes and YES need one output - "continuing" while N, No and NO require a different output - "Quit" and for all others we print "Bad input". Think about what your conditionals should be and your if statement should look something like:

    if (response == "Y" || response == "Yes" || response == "YES")

and then handle the case accordingly. You'd want to do the same for your No conditions and finally handle the case for all others. I'd suggest having your code like so:

    if( conditionals for Yes){
      //Code for Yes input
    }

    else if( conditionals for No){
      //Code for No input
    }

    else{
      //Code for all other inputs
    }

It is tempting to give you the full answer but think about how your program needs to flow and proceed from there, you've almost got it!

If you have more questions post here and we'd be glad to help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM