title: Read Integer from User
body: <p>I am trying to read an integer from terminal. Here's my code:</p>
  
  <pre><code>int readNumber()
  {
      int x;
      std::cin &gt;&gt; x;
      while(std::cin.fail())
      {
          std::cin.clear();
          std::cin.ignore();
          std::cout &lt;&lt; "Bad entry. Enter a NUMBER: ";
          std::cin &gt;&gt; x;
      }
      return x;
  }
  </code></pre>
  
  <p>Whenever I run this code I get:</p>
  
  <pre><code>Type in the number for the newsgroup that shall be deleted:
  joöä
  Bad entry.  Enter a NUMBER: Bad entry.  Enter a NUMBER: Bad entry.  Enter a NUMBER: Bad entry.  Enter a NUMBER: Bad entry.  Enter a NUMBER: Bad entry.  Enter a NUMBER: 8
  </code></pre>
  
  <p>Why does it write "bad entry" multiple times?
  If I remove <code>std::cin.clear();</code> or <code>std::cin.ignore();</code>, the program just keeps writing 
  <code>Enter a NUMBER: Bad entry.</code> Can anyone explain why it does that?</p>