If, Else If and Else

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
  int r;
  int Yes;
  int No;

    cout << "Are you alive? ";
    cin >> r;

    if (r = 'Yes') {
      cout << "Yes." << endl;
    }
    else if (r = 'No') {
      cout << "No." << endl;

    } else {
      cout << "Invalid Input. Please try again." << endl;
    }
  return 0;
}


Only started learning like two days ago. This example above was just made for testing to figure out how the If Else If and Else thing works.
What I'm trying to do is ask the user for their input. "Yes" or "No" and if anything else, to read "Invalid Input" and then loop back to the question.

For simplicity, on this testing file; I'm trying to output with "Yes." if input is "Yes" and out "No." if input is "No" and output "Error" and loop back for everything else.

Thank you!
- TruYadoo
= is the assignment operator. It sets a value.

== is a test for equality.

Do not use = when you mean ==


int r;
This is an int. That means it holds an integer. A number. How exactly do you propose to store the string 'Yes' or 'No' in it? If you want to store a string, use the string type.

The ' symbol goes around a single char, such as 'r'. You cannot put it around a multichar string. For that you must use ", like this:

if (r == "Yes")
Moschops is correct, but you don't need to have int Yes and int No. You are checking for the words, not using them as variables. They are never called in the called.
Thank you Moschops. Everything is working now.
Topic archived. No new replies allowed.