title: C++ Checking for an integer.
body: <p>New to C++. Having issues correctly looping while handling errors. I am trying to check if user input is an integer, and is positive.</p>
  
  <pre><code>do{
      cout &lt;&lt; "Please enter an integer.";
      cin &gt;&gt; n;
  
      if (cin.good())
      {
          if (n &lt; 0) {cout &lt;&lt; "Negative.";}
          else {cout &lt;&lt; "Positive.";}
      }
      else
      {
          cout &lt;&lt; "Not an integer.";
          cin.clear();
          cin.ignore();
      }
  }while (!cin.good() || n &lt; 0);
  
  cout &lt;&lt; "\ndone.";
  </code></pre>
  
  <p>When a non-integer is entered, the loop breaks. I feel like I am misunderstanding the inherent usage of <code>cin.clear()</code> and <code>cin.ignore()</code> and the status of <code>cin</code> during this loop. If I remove the <code>cin.ignore()</code>, the loop becomes infinite. Why is this? What can I do to make this into an elegantly functioning loop? Thank you.</p>