title: C++ - Sanitize Integer Whole Number Input
body: <p>I currently am using a function I found in another StackOverflow post(I can't find it), that I am using before, named "GetInt". My issue with it is that if the user inputs something like "2 2 2" it puts it into my next two Cin's. I have tried getLine, but it requires a string and I am looking for an int value. How would I structure a check to sanitize for an integer value greater than 2 and throw an error to the <code>2 2 2</code> answer.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;sstream&gt;
  #include "Board.cpp"
  #include "Player.cpp"
  
  using namespace std;
  
  int getInt()
  {
  int x = 0;
  while (!( cin &gt;&gt; x))
  {
      cin.clear();
      cin.ignore(numeric_limits&lt;streamsize&gt;::max(), '\n');
  
      cout &lt;&lt; "Please input a proper 'whole' number: " &lt;&lt; endl;
  }
  return (x);
  }
  </code></pre>
  
  <p>and my call </p>
  
  <pre><code>do
  {
      //do
      //{
          cout &lt;&lt; "How many players are there? \n";
          numberOfPlayers = getInt();
      //} while (isdigit(numberOfPlayers) == false);
  } while (numberOfPlayers &lt; 2);
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>I chose Justin's answer because it was the closest to my original code and solved the issue without major changes.</p>