Problem with if statement

This code is for class. It is supposed to ask the user if they want advice and then provide them with a random cliche sentence. The program runs, but even if the user inputs yes the program returns 0. (We were instructed to use a global array)

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
46
47
48
49
50
#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

string cliche[10] =
{
"Try to be a rainbow in someone's cloud.",
"Never miss a good chance to shut up.",
"Here's some advice. Stay alive.",
"Never ruin an apology with an excuse.",
"Don't ever take a fence down until you know why it was put up.",
"Write with the door closed, rewrite with the door open.",
"Always speak politely to an enraged dragon.",
"A wise man gets more use from his enemies than a fool from his friends.",
"Don't try to solve serious matters in the middle of the night.",
"Only boring people get bored.",
};

string advice()
{
    const int a = rand() %10 + 0;
    string adv = cliche[a];
    return adv;
}

int main()
{
    bool answer = true;
    string randAdv;
    string response;
    while (answer == true)
    {
        cout << "Would you like some advice?" << endl;
        cin >> answer;
        if (response == "yes")
        {
        randAdv = advice();
        cout << randAdv;
        }
        else
        answer = false;
    }



    return 0;
}
Line 36: You're doing a cin to a bool.

Line 37: You're checking response, but response was never initialized.

You probably meant to do cin >> response;

Should not these two be the same?
1
2
cin >> answer;
if (response == "yes")


Also keep in mind that only a response of "yes" will pass, YES, y, Y, or anything else will not.
Topic archived. No new replies allowed.