Repeats to many times!!

I cant get my script to only answer the question once! Help pls?
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
#include <iostream>
#include <string>
#include <ctime>

using namespace std;

int main ()
{
    string qString;
    
    do {
        
        srand((unsigned)time(0));
    const string qNum[] {"Yes.", "No.", "Maybe.", "Outlook good.", "Outlook, not so good.",
                         "Question too hazy!", "Possible but not probable!", "I have no idea!", "Ask again later!"};
    const int answer_count = sizeof(qNum)/sizeof(qNum[0]);
    cout << "Ask me a yes or no question!" << endl;
    cout << "Question: ";
    string qString;
    cin >> qString;
    
    int rand_answer = rand() % answer_count;
    
    cout << qNum[rand_answer] << endl;
        
    } while (qString != "Goodbye"); 
    
    return 0;    
}


Sorry for all the questions, It may seem like I am 9, I am 15 male :p
Last edited on
The qString you're testing on line 26 is the one declared on line 9 (empty) not the one on line 19.

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
#include <iostream>
#include <string>
#include <cstdlib> // for srand, rand
#include <ctime>

using namespace std;

int main ()
{
    string qString;
    
    do {
        
        srand((unsigned)time(0));
        const string qNum[] {"Yes.", "No.", "Maybe.", "Outlook good.", // fold
                             "Outlook, not so good.", "Question too hazy!",
                             "Possible but not probable!", "I have no idea!",
                             "Ask again later!"};
        const int answer_count = sizeof(qNum)/sizeof(qNum[0]);
        cout << "Ask me a yes or no question!" << endl;
        cout << "Question: ";
        //string qString; use the one on line 9
        //cin >> qString;
        getline(cin, qString); // use getline() so question can contain spaces!
    
        int rand_answer = rand() % answer_count;
    
        cout << qNum[rand_answer] << endl;
        
    } while (qString != "Goodbye"); 
    
    return 0;    
}


Also, srand() should almost always be called just once, at the start of your program. And the const data doesn't need to be in the loop.

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
#include <iostream>
#include <string>
#include <cstdlib> // for srand, rand
#include <ctime>

using namespace std;

int main ()
{
    srand((unsigned)time(0));

    const string qNum[] {"Yes.", "No.", "Maybe.", "Outlook good.", // fold
                         "Outlook, not so good.", "Question too hazy!",
                         "Possible but not probable!", "I have no idea!",
                         "Ask again later!"};
    const int answer_count = sizeof(qNum)/sizeof(qNum[0]);

    string qString;
    
    do {
        cout << "Ask me a yes or no question!" << endl;
        cout << "Question: ";
        getline(cin, qString); // use getline() so question can contain spaces!
    
        int rand_answer = rand() % answer_count;
        cout << qNum[rand_answer] << endl;
        
    } while (qString != "Goodbye"); 
    
    return 0;    
}


Andy

PS http://www.cplusplus.com/reference/string/string/getline/
Last edited on
Topic archived. No new replies allowed.