Re: Clearing the input buffer - then pausing
- From: Tom Widmer <tom_usenet@xxxxxxxxxxx>
- Date: Mon, 11 Apr 2005 11:18:26 +0100
Ray Mitchell wrote:
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.
You need to understand how console input interacts with std::cin. Firstly, you have to remember that std::cin is just a stream interface - it processes a stream of characters that might come from a pipe (via OS piping), or from a file, or from the console; std::cin can't itself tell which it is getting.
When using a console, every character that appears on the screen is inserted into the cin stream, but this only happens for a line of text when enter is pressed. So, to your example:
1:
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: ";
2: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 be3:
while ((ch = cin.peek()) != '\n' && ch != EOF) ch = cin.get();
return 0; }
With the line not commented out: The input stream is given, for example: 1\nsome random characters\n
At 1:, cin blocks until the first \n is entered. The stream buffer looks like this:
1\n
Then the cin >> value pulls the '1' off the stream. So the stream now looks like this:
\n
Now, 2 doesn't block, since it consumes characters up until the \n character in the buffer (just consuming 1 character). Now the stream is empty.
Finally, 3: (the first cin.peek call) blocks until another \n is pressed, so the buffer might now contain:
some random text\n
The get calls now pull of the characters up to the \n (which isn't consumed since you only peek at it) without blocking, so the final buffer state is just:
\n
Now lets see what happens with the line commented out. At 2:, the cin buffer is empty, so cin.ignore blocks. You enter some text and press enter, transferring the text into the cin stream:
some random text\n
Now, the ignore call pulls those characters off the stream until it hits a \n character, so the stream is now "empty".
Next, 3: blocks until a second enter is pressed, because the peek call finds an empty buffer. You enter some more characters and press enter:
some more random text\n
Finally, the peek calls unblockingly consume those characters up to the \n, leaving the final unconsumed characters as:
\n
Is that clearer? If you comment out the cin >> value, you should also comment out the ignore line.
Tom .
- Follow-Ups:
- Re: Clearing the input buffer - then pausing
- From: Ray Mitchell
- Re: Clearing the input buffer - then pausing
- References:
- Clearing the input buffer - then pausing
- From: Ray Mitchell
- Clearing the input buffer - then pausing
- Prev by Date: Re: compiler Static/dynamic
- Next by Date: Re: Read a textfile to an array
- Previous by thread: Re: Clearing the input buffer - then pausing
- Next by thread: Re: Clearing the input buffer - then pausing
- Index(es):
Relevant Pages
|