title: checking C++ string for an int: revised: clearing cin
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/514420/how-to-validate-numeric-input-c">How to validate numeric input C++</a>  </p>
  </blockquote>
  
  
  
  <p>How do you do the following:</p>
  
  <pre><code>while (iNumberOfPlayers &lt;2 || iNumberOfPlayers &gt;5)
  {
      cout &lt;&lt; "Enter number of players (1-4): ";
      cin &gt;&gt; iNumberOfPlayers;
      cin.clear();
      std::string s;
      cin &gt;&gt; s;
  }
  </code></pre>
  
  <p>After looking at the loop I'm thrown in, it looks like <code>cin</code> isn't getting reset (if I put in x) <code>cin</code> reads X again as long as I'm in the <code>while</code> loop.  Guessing this is a buffer issue, any way to clear it?</p>
  
  <p>I then tried:</p>
  
  <pre><code>while (iNumberOfPlayers &lt;2 || iNumberOfPlayers &gt;5)
  {
      cout &lt;&lt; "Enter number of players (1-4): ";
      cin &gt;&gt; iNumberOfPlayers;
      cin.clear();
      cin.ignore();
  }
  </code></pre>
  
  <p>which works except it reads everything 1 at a time.  If I put in "xyz" then the loop goes through 3 times before it stops to ask again.</p>