Clearing the Input Buffer

Using cin.sync() works great so far, but my program still breaks if you copy something with multiple lines into the console.

1
2
3
4
5
6
7
8
9
string test = "";
while(true)
{
     cin.sync();
     getline(cin, test );
        
        
     cout << endl << "test: " << test << endl;
}


However, if you were to copy this:
1
2
3

and paste it into the program, the output would be
1
2
3
test: 1

test: 2


And if you press enter one more time:
1
2
3
test: 1

test: 2


test: 3


The 3 finally pops out.


Is there any way to fix this problem?
Works for me.
1
Test: 1
2
Test: 2
3
Test: 3
The behavior of cin.sync() is implementation-defined: different compilers do different things.

Just don't use it. If you need to ignore some input, use cin.ignore, but in this example, nothing needs to be ignored.
Topic archived. No new replies allowed.