Why do I need to press enter twice to proceed? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why do I need to press enter twice to proceed?

Everything is working fine in this program except when it reaches line "cout<<"From Date: ";. At this point no matter if i give input or simply hit enter without input I have to hit enter twice to proceed further. I tried removing cin.ignore() but it causes more problem by jumping over the first getline(). Here is the snippet which is causing this problem- void Date :: diffbw(){ Date current, ref; string choice; cin.ignore(); cout<<"\n Type 'man' to enter date manually else hit Enter to insert current date!"; do{ cout<<"\n\n From Date:"; getline(cin, choice); if(choice == "man") current.getdate(); else if(choice.empty()) current = sysDate(); else{ cout<<"\n Wrong Choice!"; cout<<"\n Enter Again?(y/n): "; getline(cin, choice); } } while(choice == "y" || choice == "Y");

20th Nov 2020, 1:23 PM
Avinash Avi
Avinash Avi - avatar
2 Answers
+ 1
I don't know what your program does before executing this piece of code, nor I know C++🙄 But since cin.ignore(); ignores just a character, have you tried something like that cin.ignore(999, '\n'); instead? In any case your problem is probably just a new line left in the buffer.
20th Nov 2020, 6:17 PM
Davide
Davide - avatar
0
Davide yes i have already tried that syntax of cin.ignore(). Also i think this snippet is enough to describe the problem and remaining program has no part in it. Anyways this function is simply called from a switch statement.
20th Nov 2020, 7:18 PM
Avinash Avi
Avinash Avi - avatar