Skip to content

Instantly share code, notes, and snippets.

@manadream
Created March 21, 2015 15:50
Show Gist options
  • Save manadream/4a1aa400d45197ca3253 to your computer and use it in GitHub Desktop.
Save manadream/4a1aa400d45197ca3253 to your computer and use it in GitHub Desktop.
Clearing input buffer in C++ completely

Clearing the input buffer has proven to be difficult and elusive in my experience. There are lots of ways to do this out there, but not all of them work in all situations. Below are some methods for clearing the input buffer that I have used successfully in the past.

-cin.ignore method-

If you are trying to get input from a user (using the getline function) from the console that will end in a newline character (Enter or ‘\n’), it is not that difficult since the stream of characters will end in a newline. One thing you must account for is if the user entered more characters than the getline call’s stream size. After some testing it seemed that this worked if the entry was terminated with EOF as well. For example, consider the following statement:

cin.getline(var, 10, '\n');

This line of code will get input from the user and store the input into ‘var’ with a size limit of 10 characters. Say the user enters in over ten characters, then hits Enter, how would you check for that? One way is to check the fail bit flag from the input stream. this can be done like so:

bool failed = cin.fail();

The call cin.fail() will return true if this flag was set and false if it was not (see this for more information on the input stream status flags). If this flag is true, there was a failure in the getline operation (either because a newline character was not found before it read the specified stream size from the input buffer, meaning the input does not fit into the variable. In order to clear the input buffer after the user has entered too many characters, you will need to clear the status flags of the input stream and then ignore all cahracters up to the newline. This can be done like so:

cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');

The cin.igonre() statement takes all characters in the input stream and discards them until it reaches a newline character, which it then discards as well (see this for more info on cin.ignore()). By using the integral value defined by numeric_limits::max(), the ignore call will ignore characters with no limit until it gets to the deliminator or EOF (end of file character). In order to use this value, you must include the limits library.

#include <iostream>
#include <limits>
using namespace std;
int main(){
char var[10];
bool valid = false;
while(!valid){
cout << "Enter a string 9 characters long: ";
cin.getline(var, 10, '\n');
if(cin.fail()){
valid = false;
cout << endl << "Input exceeds variable size. Please try again." << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max, '\n');
} // end fail check
else valid = true;
} // end input loop
} // end main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment