title: how to write a function using cin.fail() to ensure valid input in main?
body: <p>Part of the requirement is that the function should not take any arguments.
  But the problem is that where can I store the cin input? I got an infinite loop here.</p>
  
  <pre><code>double getValidDouble(){
      string invalid_input;
      while (cin.fail()){
          cout &lt;&lt; "You entered a value of the wrong type!" &lt;&lt; endl;
          getline(cin,invalid_input);
          cout &lt;&lt; "Enter a double this time: " &lt;&lt; endl;  
      }
  }
  </code></pre>