Clearing the input buffer - then pausing
- From: Ray Mitchell <RayMitchell_NOSPAM_@xxxxxxxxxxxxxxxxxx>
- Date: Fri, 8 Apr 2005 18:15:02 -0700
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;
}
.
- Follow-Ups:
- Re: Clearing the input buffer - then pausing
- From: Tom Widmer
- Re: Clearing the input buffer - then pausing
- From: Frank Hickman [MVP]
- RE: Clearing the input buffer - then pausing **PORTABLY**
- From: Ray Mitchell
- Re: Clearing the input buffer - then pausing
- Prev by Date: Re: DLL callable from both C++ and VB?
- Next by Date: Re: DLL callable from both C++ and VB?
- Previous by thread: DLL callable from both C++ and VB?
- Next by thread: RE: Clearing the input buffer - then pausing **PORTABLY**
- Index(es):
Relevant Pages
|