title: How to empty the input stream in C++?
body: <p>I know how to do this in C but have no idea for a C++ solution. I want the following to be fail safe, but after providing a string or even a char to the input, the program hangs. How to read input stream including \n to free it?</p>
  
  <pre><code>int main() {
      int num;
      do { 
          std::cin.clear();
          std::cin &gt;&gt; num;
          while ( std::cin.get() != '\n' );
      } while ( !std::cin.good() || num &gt; 5 );
      return 0;
  }
  </code></pre>