After hours of research, I've finally found a way to only have to press Enter ONCE to continue.
Before, if I didn't have input I would have to press Enter twice to continue using this code. However, it works fine when I have input using cin >> , which is expected.
1 2 3 4 5 6 7 8
void wait()
{
cout << "\nPress enter to continue . . .";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin.get();
}
After fumbling around and wasting a few hours of my life, I figured a way to do it regardless of whether I have/don't have input.
1 2 3 4 5 6 7 8 9 10
void wait()
{
cout << "\nPress enter to continue . . .";
cin.clear(); // clear the error flags set in the input stream
cin.ignore(cin.rdbuf()->in_avail()); // ???
char ch[1];
cin.getline(ch, 1);
}
But I don't know how it works, especially cin.ignore(cin.rdbuf()->in_avail()) .
Anyone care to explain?
constauto chars_available = std::cin.rdbuf()->in_avail() ;
if( chars_available > 0 ) // if there are some characters in the input buffer
std::cin.ignore(chars_available) ; // extract and discard the characters