title: How do I implement single line integer type command line input validation using cin?
body: <p>I have a program which asks the user to input an integer in the range [0,2].  I used the following link as a guide.</p>
  
  <p><a href="https://stackoverflow.com/questions/2075898/good-input-validation-loop-using-cin-c">Good input validation loop using cin - C++</a> </p>
  
  <p>However, when the user presses enter without inputting data the cursor simply goes to the next line in the command prompt whereas I would prefer it to prompt the user to enter a valid number.  Does prompting the user in this case make sense, or is there a reason not to implement validation as single line input to begin with?  In the case of strings I would use getline to solve this, should I use that somehow in this case?  Here is my code, based on the above link:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  int main()
  {
      int answeredNumber;
      while(1)
      {
          std::cout &lt;&lt; "Enter your answer: ";
          if(std::cin &gt;&gt; answeredNumber &amp;&amp; answeredNumber &gt;= 0 &amp;&amp; answeredNumber &lt;= 2)
          {
              break;
          }
          else
          {
              std::cout &lt;&lt; "Please enter a valid answer: " ;
              std::cin.clear();
            std::cin.ignore(std::numeric_limits&lt;std::streamsize&gt;::max(), '\n');
          }
      }
      system("pause");
  }
  </code></pre>