How do I flush the cin buffer?

C++CinIo Buffering

C++ Problem Overview


How do I clear the cin buffer in C++?

C++ Solutions


Solution 1 - C++

I would prefer the C++ size constraints over the C versions:

// Ignore to the end of file
std::cin.ignore(std::numeric_limits<std::streamsize>::max())

// Ignore to the end of line
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n')

Solution 2 - C++

Possibly:

std::cin.ignore(INT_MAX);

This would read in and ignore everything until EOF. (you can also supply a second argument which is the character to read until (ex: '\n' to ignore a single line).

Also: You probably want to do a: std::cin.clear(); before this too to reset the stream state.

Solution 3 - C++

cin.clear();
fflush(stdin);

This was the only thing that worked for me when reading from console. In every other case it would either read indefinitely due to lack of \n, or something would remain in the buffer.

EDIT: I found out that the previous solution made things worse. THIS one however, works:

cin.getline(temp, STRLEN);
if (cin.fail()) {
    cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
}

Solution 4 - C++

I have found two solutions to this.

The first, and simplest, is to use std::getline() for example:

std::getline(std::cin, yourString);

... that will discard the input stream when it gets to a new-line. Read more about this function here.

Another option that directly discards the stream is this...

#include <limits>
// Possibly some other code here
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');

Good luck!

Solution 5 - C++

int i;
  cout << "Please enter an integer value: ";

  // cin >> i; leaves '\n' among possible other junk in the buffer. 
  // '\n' also happens to be the default delim character for getline() below.
  cin >> i; 
  if (cin.fail()) 
  {
    cout << "\ncin failed - substituting: i=1;\n\n";
    i = 1;
  }
  cin.clear(); cin.ignore(INT_MAX,'\n'); 

  cout << "The value you entered is: " << i << " and its double is " << i*2 << ".\n\n";

  string myString;
  cout << "What's your full name? (spaces inclded) \n";
  getline (cin, myString);
  cout << "\nHello '" << myString << "'.\n\n\n";

Solution 6 - C++

How about:

cin.ignore(cin.rdbuf()->in_avail());

Solution 7 - C++

I prefer:

cin.clear();
fflush(stdin);

There's an example where cin.ignore just doesn't cut it, but I can't think of it at the moment. It was a while ago when I needed to use it (with Mingw).

However, fflush(stdin) is undefined behavior according to the standard. fflush() is only meant for output streams. fflush(stdin) only seems to work as expected on Windows (with GCC and MS compilers at least) as an extension to the C standard.

So, if you use it, your code isn't going to be portable.

See <https://stackoverflow.com/questions/2979209/using-fflushstdin>;.

Also, see <http://ubuntuforums.org/showpost.php?s=9129c7bd6e5c8fd67eb332126b59b54c&p=452568&postcount=1> for an alternative.

Solution 8 - C++

Another possible (manual) solution is

cin.clear();
while (cin.get() != '\n') 
{
    continue;
}

I cannot use fflush or cin.flush() with CLion so this came handy.

Solution 9 - C++

Easiest way:

cin.seekg(0,ios::end);
cin.clear();

It just positions the cin pointer at the end of the stdin stream and cin.clear() clears all error flags such as the EOF flag.

Solution 10 - C++

It worked for me. I have used for loop with getline().

cin.ignore()

Solution 11 - C++

The following should work:

cin.flush();

On some systems it's not available and then you can use:

cin.ignore(INT_MAX);

Solution 12 - C++

#include <stdio_ext.h>

and then use function

__fpurge(stdin)

Solution 13 - C++

cin.get() seems to flush it automatically oddly enough (probably not preferred though, since this is confusing and probably temperamental).

Solution 14 - C++

fflush(stdin) − It is used to clear the input buffer memory. It is recommended to use before writing scanf statement.

fflush(stdout) − It is used for clearing the output buffer memory. It is recommended to use before printf statement. The following should work:

cin.flush(); On some systems it's not available and then you can use:

cin.ignore(INT_MAX); Both Windows and Linux define the behaviour of fflush() on an input stream, and even define it the same way (miracle of miracles). The POSIX, C and C++ standards for fflush() do not define the behaviour, but none of them prevent a system from defining it. If you're coding for maximum portability, avoid fflush(stdin); if you're coding for platforms that define the behaviour, use it — but be aware that it is not portable portable code does not use fflush(stdin). Code that is tied to Microsoft's platform may use it and it may work as expected, but beware of the portability issues.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTalView Question on Stackoverflow
Solution 1 - C++Martin YorkView Answer on Stackoverflow
Solution 2 - C++Evan TeranView Answer on Stackoverflow
Solution 3 - C++jyggorathView Answer on Stackoverflow
Solution 4 - C++BenView Answer on Stackoverflow
Solution 5 - C++user187046View Answer on Stackoverflow
Solution 6 - C++asdfView Answer on Stackoverflow
Solution 7 - C++Shadow2531View Answer on Stackoverflow
Solution 8 - C++Bobul MentolView Answer on Stackoverflow
Solution 9 - C++Chaitanya VaishampayanView Answer on Stackoverflow
Solution 10 - C++Amrit MallaView Answer on Stackoverflow
Solution 11 - C++Gunnar SteinnView Answer on Stackoverflow
Solution 12 - C++techcompView Answer on Stackoverflow
Solution 13 - C++GuestPerson001View Answer on Stackoverflow
Solution 14 - C++shyed2001View Answer on Stackoverflow