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);
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> .
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);
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.