Cin Being Skipped

Dec 4, 2016 at 2:04am
My code is below. The first getline prompt is being skipped entirely, but the second is recognized by the program. I have tried cin.clear() already. Any suggestions?

1
2
3
4
5
6
string first_question;
string second_question;
cout << "Enter a new animal in the form of a question, e.g., 'Is it a whale?': \n";
getline(cin, first_question);
cout << "\nNow enter a question for which the answer is 'yes' for your new animal, and which does not contradict your previous answers: ";
getline(cin, second_question);
Last edited on Dec 4, 2016 at 2:05am
Dec 4, 2016 at 2:12am
Have you used formatted input(cin >> ...) before this section of code? If so, you'll need to do cin.ignore( numeric_limits<streamsize>::max( ), '\n' ) before getline(), as formatted input leaves a newline feed in cin.
Make sure to #include <limits> .
Dec 4, 2016 at 2:20am
Thanks for the response integralfx. Yes, I have used formatted input earlier in the code. I did as you said, but to no avail. This is my updated code:

1
2
3
4
5
6
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Enter a new animal in the form of a question, e.g., 'Is it a whale?': \n";
getline(cin, first_question);
cout << "\nNow enter a question for which the answer is 'yes' for your new animal, and which does not contradict your previous answers: ";
getline(cin, second_question);
Last edited on Dec 4, 2016 at 2:20am
Dec 4, 2016 at 2:31am
Can you please post your full code so that I can diagnose the problem.
Dec 4, 2016 at 3:16am
Cout what the user entered right after its entered to debug and check that its not receiving gibberish. (or use a break point and check what the value is during the run)
This should typically be your first check when you have input errors that dont cause compiler errors.
Topic archived. No new replies allowed.