"I can tell you the future" problem

So my program compiles but when the user types in like "What's for dinner?" the whole time goes crazy and repeats everything till I stop it. But if I do "What'sfordinner?" it works perfectly fine. What did I do wrong?

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
#include <iostream>
#include <stdlib.h> 
#include <time.h>
using namespace std;

int main() {

int inNum;
int n;

string x = "yes";
string question="";

string answers[8] = {
"No",
"Yes",
"Your guess is as good as mine",
"It looks like there's a chance",
"If you believe anything is possible",
"Try again","Give me another one","I believe it's your lucky day"
};

while ( x == "yes" ) {
cout << "Ask me a question and I'll tell you the future" << endl;
cin >> question;
cout << "Choose between 1-8 or type 9 for random:" << endl;
cin >> inNum;

n = inNum;

if ( n==9 )
{
srand(time(NULL));
n = rand() % 8 + 1; }

if ( n > 0 and n < 9 ){ cout << answers[n] << endl; }

if ( n > 9 ){ cout << "That's not between 1-9!" << endl;
}
cout << "Do you want to play again?" << endl;
cin >> x;
}
return 0;
}
The problem is that cin >> question; only reads one word and leaves the rest in the input stream.
Then, when you get to cin >> inNum;, it tries to read in the rest of what you typed in for the first question and chokes up because that's not an int.
(Try inputting "What's 5for dinner?" to see what I mean)

The solution is to use getline instead:
getline(cin, question);
http://www.cplusplus.com/reference/string/string/getline/

But do be careful of mixing getline and cin >> something, because cin >> something will leave the newline character (from you pressing Enter) in the input stream, but getline stops immediately after it sees a newline character.
A "quick fix" (though it doesn't work in all cases) is to put cin.ignore(); after doing cin >> something; but before doing getline(cin, somethingelse);.
Ok I got it to do the sentences, but now the loop isn't acting right. It combines the "ask me a question..." and "Choose between..". If I put in a sentence it'll go crazy.
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
#include <iostream>
#include <stdlib.h> 
#include <time.h>
using namespace std;

int main() {

int inNum;
int n;

string x = "yes";
string question="";

string answers[8] = {
"No",
"Yes",
"Your guess is as good as mine",
"It looks like there's a chance",
"If you believe anything is possible",
"Try again","Give me another one","I believe it's your lucky day"
};

while ( x == "yes" ) {
cout << "Ask me a question and I'll tell you the future" << endl;
getline(cin, question);
cin.ignore();

cout << "Choose between 1-8 or type 9 for random:" << endl;
cin >> inNum;

n = inNum;

if ( n==9 )
{
srand(time(NULL));
n = rand() % 8 + 1; }

if ( n > 0 and n < 9 ){ cout << answers[n] << endl; }

if ( n > 9 ){ cout << "That's not between 1-9!" << endl;
}
cout << "Do you want to play again?" << endl;
cin >> x;
}
return 0;
}
You just ran into the problem with mixing getline and cin >> something that I described earlier.

The problem is that after it asks you if you want to play again, you have cin >> x;, and when it loops back around, the call to getline eats the leftover newline character instead of waiting for an input.
Topic archived. No new replies allowed.