title: C++ stringstream error handling
body: <p>I'm trying to error handle the conversion from string to int using ss.fail and after the program loops back to the input, it keeps giving an error even if I enter integers. Note that this only happens after the error handle loops. I've tried ss.clear(), cin.ignore, etc and it still loops indefinitely when I enter an integer. How do I correctly error handle this?</p>
  
  <pre><code>string strNumber;       //User to input a number
  stringstream ss;        //Used to convert string to int
  int number;             //Used to convert a string to number and display it
  
  bool error = false;          //Loops if there is an input error
  
  do{
      //Prompts the user to enter a number to be stored in string
      cout &lt;&lt; endl &lt;&lt; "Enter an integer number: ";
      getline(cin, strNumber);
  
      //Converts the string number to int and loops otherwise
      ss &lt;&lt; strNumber;
      ss &gt;&gt; number;
  
      if(ss.fail()){
          cout &lt;&lt; "This is not an integer" &lt;&lt; endl;
          ss.clear();
          //cin.ignore(numeric_limits&lt;streamsize&gt;::max(), '\n');
          error = true;
      }
      else
          error = false;
  
  } while(error == true);
  </code></pre>