Trouble with if statements and strings

I am just trying to store an answer of yes or no from user, but the program doesn't switch to the correct if statement. No matter what the user inputs, control passes to the first if statement. So even if the answer is No, it still goes on to the if statement for if Yes were to have been entered.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  // practice

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

int main()
{
    string uGame;

    // Stores user's name
    string UserName;
    cout << "Hello user. Please enter your name." <<endl;
    cin >> UserName;

   //Option to play or terminate
    string answer;
    cout << "Nice to meet you " << UserName << ". Would you like to play a game with me?" <<endl;
    cout << "" <<endl;
    cout << "Please type Yes or No  ";
    cin >> answer;

    if (answer == "Yes" , "YES" , "yes")
    {
       cout << "Fantastic! Would you like to play a Math or Word game?";
        cin >> uGame;
    }
    if (answer == "No" , "NO" , "no")
    {
       cout << "I am sorry to hear that.";
    }


}
Last edited on
closed account (48T7M4Gy)
1
2
3
 if (answer == "Yes" || answer == "YES" || answer == "yes")

if (answer == "No" || answer == "NO" || answer == "no")
Last edited on
Awesome thanks a lot!
You can only use one letter (or two) as string variable i.e Y for YES or N for NO.
For you to use letter more than one you have to use an array.
Topic archived. No new replies allowed.