Why is the buffer not empty?
When I run the .exe for this console app without the statement
cin.ignore(1000, '\n');
the window closes when I enter the letter.
If I include the statement above, the window stays open, which is what I want. The output is this:
1 2 3 4
|
Type a letter:
v
You typed v
Press any key to quit.
|
I don't understand why the behavior is different. It implies there's something in the buffer that needs to be ignored, that's feeding
What would that be? I thought
|
cout << "Press any key to quit.\n";
|
would flush the buffer.
Here's the program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
#include <iostream>
using namespace std;
int main()
{
char letter = 'x';
cout << "Type a letter:\n";
cin >> letter;
cout << "You typed " << letter << '\n';
cin.ignore(1000, '\n');
cout << "Press any key to quit.\n";
cin.get();
return 0;
}
|
> It implies there's something in the buffer that needs to be ignored, that's feeding cin.get();
Yes, there would be (at least) a new line left in the input buffer after formatted input with
cin >> letter;
that is feeding
cin.get();
Formatted input:
http://www.cplusplus.com/forum/beginner/281072/#msg1215707
Thanks so much.
Topic archived. No new replies allowed.