how to clear cin buffer

Solutions on MaxInterview for how to clear cin buffer by the best coders in the world

showing results for - "how to clear cin buffer"
Lilia
12 Oct 2018
1int number;
2
3cout << "Enter an Integer: ";
4cin >> number;
5
6// User types any char or string of length < 100
7
8// Because input stream is in a failed state, cin will be evaluated to false
9while ( !cin )
10{
11    cin.clear ();    // Restore input stream to working state
12    cin.ignore ( 100 , '\n' );    // Get rid of any garbage that user might have entered
13    cout << "I said enter an integer, Dumbass. Try again: ";
14    cin >> number;    // After cin is restored and any garbage in the stream has been cleared, store user input in number again
15}
similar questions
console clear c 2b