How do I flush the cin buffer?

Tal picture Tal · Nov 2, 2008 · Viewed 309.1k times · Source

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

Answer

Martin York picture Martin York · Nov 2, 2008

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

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

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