How to clear the buffer for istreams?

Jul 4, 2024 at 8:59pm
Hi,

what if I want to read only one character of input from the keyboard and want to use streams? so this:

1
2
char c;
cin >> x;


will read a character plus any other garbage the user entered. To clear the buffer I must now do this:

1
2
string str;
getline(cin, str);


Is there a cleaner one instruction way that would discard the values in the buffer?

Jul 5, 2024 at 3:45am
Registered users can post here. Sign in or register to post.