Beginners mistake

This is a completed homework assignment for which I received full credit:

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
#include <iostream>
#include <string>
using namespace std;

int main()

{
    string firstName;
    string number;
    string animal;
    string country;
    string secondAnimal;
    string color;

    cout << "Please enter your first name: ";
    cin >> firstName;
    cout << "\nHello, " << firstName << "! Welcome to funny headlines!" << "\n" << endl;
    cout << "I'm going to ask you five questions." << "\n" << endl;

    cout << "1.) Please think of a number greater than one. What is it? ";
    cin >> number;
    cout << "2.) Think of a animal. What animal are you thinking of? ";
    cin >> animal;
    cout << "3.) Think of a country. What is it? ";
    cin >> country;
    cout << "4.) Think of another animal. What is it? ";
    cin >> secondAnimal;
    cout << "5.) What color comes to mind? ";
    cin >> color;

    cout << "\nGreat! Here are your funny headlines:" << endl;
    cout << "\nTOP STORY: " << animal << " announces US presidential bid with high hopes." << endl;
    cout << "\nGreat Lakes turn " << color << ". Authorities believe " << secondAnimal <<"s to blame." << endl;
    cout << "\nEnd of the world in " << number << " days? It's true, says Pope." << endl;
    cout << "\nCanada declares war on " << country << ". Authorities believe " << secondAnimal << "s to blame. " << endl;

    return 0;
}

However, one little tidbit is bothering me. I noticed that when I put "United States" for country, since there is a space it takes "United" and then jumps to the next question. Reading chapter three right now states that getline() can fix this problem, if I'm reading this correctly. For example:

1
2
3
4
string movieName;
cout << "Enter movie name: ";
getline(cin, movieName);
cout << endl;


So I went back to try this:

1
2
3
    cout << "3.) Think of a country. What is it? ";
    getline(cin, country);
    cout << "4.) Think of another animal. What is it? ";


as well as several other variations:

1
2
3
4
    cout << "3.) Think of a country. What is it? ";
    cin >> country;
    getline(cin, country);
    cout << "4.) Think of another animal. What is it? ";


1
2
3
4
    cout << "3.) Think of a country. What is it? ";
    getline(cin, country);
    cout << endl;
    cout << "4.) Think of another animal. What is it? ";


and it is still not doing what I want it to. It will ask the question and skip to the next, not asking for any input for the string "country" whatsoever, or just pick up " States" and apply it to one string, albeit the wrong one. Thoughts?
Last edited on
when you use operator >> for input it will leave a new line in the buffer you must ignore that before using getline because getline reads until it finds a newline therefore it would not read anything.

1
2
3
4
5
6
7
8
9
cin >> something;

//http://www.cplusplus.com/reference/istream/istream/ignore/?kw=cin.ignore
//http://www.cplusplus.com/reference/limits/numeric_limits/?kw=numeric_limits 
//^ you need to include limits --> #include <limits>

cin.ignore( std::numeric_limits<streamsize>::max() , '\n' );

getline( cin , something_else );


Here is a link that might explain better:

http://www.cplusplus.com/forum/articles/6046/
Last edited on
Your last input operation, cin >> animal;, leaves a newline in the input buffer (remember, you had to press Enter to send the input to the program -- that button press translates to a '\n' character in the input buffer).

What getline does is read input until it reaches a newline character (which is discarded). Because of your cin >> animal; line, which left a newline character in the input buffer, the first thing getline finds is that newline character, so it eats it and moves on before you ever get a chance to move your fingers.

For a quick fix: put cin.ignore(); or cin.get(); immediately after cin >> animal;. This will ignore/read one character from the input buffer (which should hopefully be the extra newline character).

A better fix is to put cin.ignore(numeric_limits<streamsize>::max(), '\n'); after cin >> animal; (you'll also need to #include <limits> for that).
That way, cin will discard all characters left in the input buffer, until it reaches (and discards) a newline character.
So, if you were to enter, for instance, "Giant panda" for the animal, it would read the "Giant" and then discard the " panda\n" (which, although it may not be what you want, at least it's better than the animal name being "Giant" and the country name becoming " panda" without asking you first).
(In comparison, if you just used the "quick fix" above, the cin.ignore(); would have only eaten one character (the space after the "Giant"), so it would automatically read the remaining "panda" into the country variable.)
Last edited on
Interesting. Thanks
Topic archived. No new replies allowed.