-4

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;
}
daoz1
  • 43
  • 1
  • 6
  • Well, from the looks of it, you have to handle all other possible inputs that have more than one character. As written, your program seems to handle just one character inputs, and the problem seems pretty clear. You obviously can't use a single `char` to store "YES" or "NO", so you need to rethink your overall strategy. – Sam Varshavchik Oct 07 '18 at 00:47
  • it seems almost there. besides what @Sam Varshavchik mentioned, you may need a`while` or `do while` loop for keep asking '"Do you wish to continue?" – Yucel_K Oct 07 '18 at 00:50
  • we haven't learned loops yet :/ – daoz1 Oct 07 '18 at 02:30
  • @SamVarshavchik so should I do string alongside char? – daoz1 Oct 07 '18 at 02:53
  • 1
    Well, what you should've done is just tried that, instead of waiting for some stranger to visit stackoverflow.com a day later. – Sam Varshavchik Oct 07 '18 at 12:48

3 Answers3

1

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.

Daejichu
  • 57
  • 8
  • we haven't learned loops yet, and what do you mean by "including string and changing the response to take a string from the user for 'Y', "Yes", etc." ? – daoz1 Oct 07 '18 at 02:57
  • @daoz1 loops are one of the most important control structures you'll learn and if you put the time in, you'll jump ahead of the class! Remember this moment as it'll be an important one. Determining your data type/structure ahead of time is crucial to good programming. In your assignment, a requirement is to accept 'Y', "Yes", etc. The type 'char' is perfect for 'Y', but cannot hold strings ("Yes", "SpongBob", "Trump", etc.). Hint: include then you can use as a type! – Daejichu Oct 07 '18 at 04:47
0

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;
}
  • Formally `std::tolower(response[i])` is not legal code and will invoke undefined behaviour. Also, what's with the weird off again on again use of `std::`? – George Oct 07 '18 at 01:17
  • Additional "std ::" were not necessary, I removed them. – Vincent Rougeau-Moss Oct 07 '18 at 01:23
  • 1
    [`tolower(response[i])`](https://en.cppreference.com/w/cpp/string/byte/tolower) is still illegal. "Like all other functions from , the behavior of std::tolower is undefined if the argument's value is neither representable as unsigned char nor equal to EOF." – George Oct 07 '18 at 01:25
  • For user input using the keyboard, EOF or values ​​not represented by an unsigned character can not occur. I can try any key on the keyboard (including F1 to F12), but it will still work properly. All we need is just "yes" / "no". For a more complex use, this question has a good answer: https://stackoverflow.com/questions/34433380/lowercase-of-unicode-character – Vincent Rougeau-Moss Oct 07 '18 at 01:37
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!

sinIqIn
  • 37
  • 2
  • 10