I am using cin to input a single character from keyboard.The problem is that if 2 or more characters are entered then the rest of the characters are being considered as input for the further cin statements.Is there any way in which I can clear the inpu buffer memory after every cin statement so that rest of the characters are removed from the buffer.


char ch;
cout<<"\nInput a character:";
cout<<"\nInput another character:";

In the above example if two characters are entered at the first instance then the second character is considered as the input for the second cin statement.
I have tried using flush(), eof() and ignore() to no effect.
How to avoid this?Any help is highly appreciated.

8 Years
Discussion Span
Last Post by Lerner

Read Narue's post

Also, it may be more beneficial for you to ignore a specified amount of characters or ignore characters until a specified delimiter is met--

#include <iostream>

using std::cin;

int main(){

    cin.ignore(INT_MAX, '\n');

    return 0;

If you put cin in an error-state, you will have to pull it out of its error-state first before attempting to ignore characters.

Another example of ignore

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.