If/Then Statement Issues?

I picked up C++ as a beginning programming language after moving on from Scratch a couple of years ago. I've been working on my current project for a little while now and can't figure out how to structure my if/then statement.

For clarity, I want to give the computer a list of phrases (prophecies from Portal 2!) and have it give them back to me randomly-- it would pull one item off of the list and spit it out. I coded it on Scratch so I have a rough idea of what I need to do-- there's a loop involved, an if/then statement, and I obviously have to give the computer the list-- but just the if/then statement alone is bugging me.

I’d like to have the user input yes or no, which would be stored in a string called “answerProp”, and I’d like that to determine the if/then statement, but it keeps giving me an error message.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

using namespace std;

int main()
{
    string answerProp;
    cout << "Do you have a question?: " << endl;
    cin >> answerProp;
    if (answerProp = yes) {
        cout << "The answer is beneath us." << endl;
    }
        else {
            cout < "That's all I can say." << endl;
        }
    return 0;
}
if (answerProp == "yes") //what you had was looking for variable yes, not the string yes. and == is comparison, = is assignment and IT WILL WORK AS IF TRUE for your code but it is a bug
also c++ is cased, so Yes is not yes is not YES ...
you can upcase or downcase and check that way if you care.
Last edited on
@jonnin Ooh, thank you! I'll be sure to check it out!
Hello paracosmadd,

In addition to what jonnin has said your if statement should really start something like:
if (answerProp == "yes" || answerProp == "Yes" || ...)and continue with every possible combination you can think of and the one you miss will be what the first user tries to use.

A better solution could be:
1
2
3
4
5
6
   char answerProp;

    cout << "Do you have a question?: " << endl;
    cin >> answerProp;

    if (answerProp == 'y' || answerProp == 'Y')

This way you only have two choices.

Another option:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <cctype>

using namespace std;

int main()
{
    char answerProp;

    cout << "Do you have a question?: " << endl;

    cin >> answerProp;

    if (std::tolower(answerProp) == 'y' )

Or you could use "std::toupper()" and a capital 'Y'.

Keep it simple. Do not expect a user to do what you want. Even with trying to account for every possible combination of capital and lower case letters there is still typos and spelling to consider.

Hope that helps,

Andy
This got me the result I wanted, thank you!
Nobody can ever trust the user... I can't even trust me when it comes to testing my own applications.
You can write int as the place of char like:
Int answerProp;
and also
if (answerProp == yes) will be used.

Freshers who want to know more about c language programming or other like PHP, Python, Android, Java, ML then visit our page: http://www.cetpainfotech.com/technology/C-Language-Training
Do not listen to spam poster, its random text.
Topic archived. No new replies allowed.