Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

iostream: Clearing the cin buffer

Status
Not open for further replies.

chpicker

Programmer
Apr 10, 2001
1,316
Does anyone know how to clear the input buffer of the cin object? Take this example:
Code:
char Selection;
cout<<&quot;Please select a letter: &quot;;
cin>>Selection;
cout<<&quot;You chose &quot;<<Selection<<endl;
cout<<&quot;Now choose another: &quot;;
cin>>Selection;
cout<<&quot;You chose &quot;<<Selection<<endl;
return;
What ends up happening is this. If the user enters 2 characters at the first prompt, the compiler assigns the first character to Selection and holds onto the second. When the second cin is reached, the program won't wait for the user, since it already has that second character in the buffer. It simply assigns that second character to the second Selection and the program continues on its way.

What I want it to do is take the first character entered by the user and discard the rest. Does iostream have a built-in way to do this?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top