title: Integer validation for input
body: <p>I tried to prompt user for input and do the validation. For example, my program must take in 3 user inputs. Once it hits non-integer, it will print error message and prompt for input again. Here is how my program going to be look like when running :</p>
  
  <blockquote>
    <p>Enter number: a </p>
    
    <p>Wrong input</p>
    
    <p>Enter number: 1</p>
    
    <p>Enter number: b</p>
    
    <p>Wrong input</p>
    
    <p>Enter number: 2</p>
    
    <p>Enter number: 3</p>
    
    <p>Numbers entered are 1,2,3</p>
  </blockquote>
  
  <p>And here is my code:</p>
  
  <pre><code>double read_input()
  {
      double input;
      bool valid = true;
      cout &lt;&lt; "Enter number: " ;
      while(valid){
          cin &gt;&gt; input;
          if(cin.fail())
          {
              valid = false;
          }
      }
      return input;
  }
  </code></pre>
  
  <p>My main method:</p>
  
  <pre><code>int main()
  {
  double x = read_input();
  double y = read_input();
  double z = read_input();
  }
  </code></pre>
  
  <p>When my first input is non-integer, the program just exits by itself. It does not ask for prompting again. How could I fixed it? Or am I supposed to use a do while loop since I asking for user input.</p>
  
  <p>Thanks in advance.</p>