title: Checking input is an integer
body: <p>I am trying to ensure user input is a positive integer and have got my function to work no matter what the user tries to put in.</p>
  
  <pre><code>int getNumber()
  {
      string userInput;
      int userNumber;
      bool badInput = true;
      cout&lt;&lt;"Enter a positive integer: ";
      cin&gt;&gt;userInput;
      while (badInput)
      {
          for (int i=0; i&lt;userInput.length(); i++)
          {
              if (isdigit(userInput[i]))
              {
                  badInput = false;
              }
              else
              {
                  badInput = true;
                  cout&lt;&lt;"That wasn't a valid input, try again: ";
                  cin&gt;&gt;userInput;
                  break;
              }
          }
      }
      userNumber = atoi(userInput.c_str());
      return userNumber;
  }
  </code></pre>
  
  <p>Is there a cleaner way to do this or is this the best way? I tried various other methods such as using cin.bad etc but they always managed to miss some issue.</p>