Help me with If statements but with text !?

#include <iostream>

using namespace std;

int main()
{
int answer;
int answer2;
cout<<"Do you want to answer a yes or no question ? ";
cin>>answer;
if (answer == "yes")
{
cout<<"Great\n";
cout<<"Now, the question is: have you ever lied ? ";
cin>>answer2;
if (answer2 == "yes")
{
cout<<"Thank you for your honesty.";
}
if else(answer2 == "no")
{
cout<<"That is a lie sir ! ";
}
}
if else(answer == "no")
{
cout<<"You just answered a yes or no question !";
}
return 0;
}
Line 11,16,20,25: You can't compare an int to a string.

Line 20,25: if else is not valid. Did you mean else if ?

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
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
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <string>

using namespace std;

int main()
{
    // int variables can only hold numbers. If you want variables to hold "yes" or "no", use a string.
    string answer;
    string answer2;

    cout<<"Do you want to answer a yes or no question? ";
    cin>>answer;

    if (answer == "yes")
    {
        cout<<"Great\n\n"; // Added an extra line to make it look better
        cout<<"Now, the question is: have you ever lied? ";
        cin>>answer2;

        if (answer2 == "yes")
        {
            cout<<"Thank you for your honesty.";
        }
        else if (answer2 == "no") // Use else if instead of if else
        {
            cout<<"That is a lie sir!";
        }
        else // Add this line just in case someone inputs something other than "yes" or "no"
        {
            cout<<"Could not understand input.";
        }
    }
    else if (answer == "no")
    {
        cout<<"\nYou just answered a yes or no question!"; // Added an extra line to make it look better
    }
    else // Add this line just in case someone inputs something other than "yes" or "no"
    {
        cout<<"Could not understand input.";
    }
    cout << endl; // Added an extra line to make it look better

    return 0;
}
Last edited on
closed account (3voN6Up4)
I was actually wondering the same thing, thanks PBachmann.

To clarify:

1
2
= is for equations?
== is to distinguish variables?

Last edited on
@Lucsofhazard

= is an assignment. You can think of it as an arrow:
1
2
//num <- 4;
  num = 4;

This tells the program "assign 4 to the variable num.

== is a true/false question. This returns true or false (1 or 0)
1
2
//is num equal to 4?
       num == 4;
closed account (3voN6Up4)
Oh okay, so for if statements too?

 
if (num == 5)


like if/else
Last edited on
@Lucsofhazard
Correct. Always put true/false values in if(). Any number (positive or negative) other than 0 is considered true.
= is for equations?

It's best to not think of the = as having anything to do with equations.

For example, in C++ you can put
 
    x = x + 1;
and it makes complete sense. Calculate the value of the expression x+1 and assign the result to the variable x.

As a mathematical equation it would be nonsense ( it would simplify to 1 = 0).
Topic archived. No new replies allowed.