Clearing the input buffer - then pausing

Tech-Archive recommends: Fix windows errors by optimizing your registry



VS .NET 2003: I should know this, but I don't! I would like to pause my
program at some arbitrary point and resume only when the Enter key is pressed
once (preceeded by zero or more other characters). The code below works if I
do not comment out the first cin line. If I do, however, it takes two
strokes of the Enter key. What appears to be happening is that the ignore
method does not operate on a new buffer, but that's just a guess. I've tried
several different things but it has become painfully clear that I should
consider another line of work! Additionally, it seems there must be less
complex way to accomplish this task. Thanks.

Note: cin.ignore(INT_MAX, '\n');
It seemed to me that cin.ignore(INT_MAX); would be more appropriate, but it
is worse.

#include <iostream>
#include <climits>
using namespace std;

int main()
{
int value = 0;
cout << "Enter an intergal value: ";
cin >> value; // Requires 2nd Enter if this line commented out
cout << "The value was " << value << endl;

int ch;
cin.ignore(INT_MAX, '\n'); // It seemed that cin.ignore(INT_MAX); would be
while ((ch = cin.peek()) != '\n' && ch != EOF)
ch = cin.get();

return 0;
}

.



Relevant Pages