title: What is the best way to do input validation in C++ with cin?
body: <p>My brother recently started learning C++. He told me a problem he encountered while trying to validate input in a simple program. He had a text menu where the user entered an integer <code>choice</code>, if they entered an invalid choice, they would be asked to enter it again (do while loop). However, if the user entered a string instead of an int, the code would break.
  I read various questions on stackoverflow and told him to rewrite his code along the lines of:  </p>
  
  <pre><code>#include&lt;iostream&gt;
  using namespace std;
  
  int main()
  {
      int a;
      do
      {
      cout&lt;&lt;"\nEnter a number:"
      cin&gt;&gt;a;
          if(cin.fail())
          {
              //Clear the fail state.
              cin.clear();
              //Ignore the rest of the wrong user input, till the end of the line.
              cin.ignore(std::numeric_limits&lt;std::streamsize&gt;::max(),\
                                                      '\n');
          }
      }while(true);
      return 0;
  }
  </code></pre>
  
  <p>While this worked ok, I also tried a few other ideas:<br>
  1. Using a try catch block. It didn't work. I think this is because an exception is not raised due to bad input.
  2. I tried <code>if(! cin){//Do Something}</code> which didn't work either. I haven't yet figured this one out.<br>
  3. Thirdly, I tried inputting a fixed length string and then parsing it. I would use atoi(). Is this standards compliant and portable? Should I write my own parsing function?<br>
  4. If write a class that uses cin, but dynamically does this kind of error detection, perhaps by determining the type of the input variable at runtime, would it have too much overhead? Is it even possible?</p>
  
  <p>I would like to know what is the best way to do this kind of checking, what are the best practices?</p>
  
  <p>I would like to add that while I am not new to writing C++ code, I am new to writing good standards compliant code. I am trying to unlearn bad practices and learn the right ones. I would be much obliged if answerers give a detailed explanation.</p>
  
  <p><strong>EDIT</strong>: I see that litb has answered one of my previous edits. I'll post that code here for reference.</p>
  
  <pre><code>#include&lt;iostream&gt;
  using namespace std;
  
  int main()
  {
      int a;
      bool inputCompletionFlag = true;
      do
      {
      cout&lt;&lt;"\nEnter a number:"
      cin&gt;&gt;a;
          if(cin.fail())
          {
              //Clear the fail state.
              cin.clear();
              //Ignore the rest of the wrong user input, till the end of the line.
              cin.ignore(std::numeric_limits&lt;std::streamsize&gt;::max(),\
                                                      '\n');
          }
          else
          {
              inputCompletionFlag = false;
          }
      }while(!inputCompletionFlag);
      return 0;
  }
  </code></pre>
  
  <p>This code fails on input like "1asdsdf". I didn't know how to fix it but litb has posted a great answer. :)</p>