title: C++ How to check an input float variable for valid input
body: <p>I'm writing a program that acts as a calculator; based on the character input by the user it performs a certain operation. The structure of the program seems to work fine, but I'd like to be able to check for erroneous input. After receiving the float variable, is there any way to check if it does not contain any characters other than digits and decimals? I've tried <code>isdigit</code>, and this: </p>
  
  <pre><code>if (!(cin &gt;&gt; x)) {
      cout &lt;&lt; "You did not enter a correct number!" &lt;&lt; endl; 
      return;
  }
  </code></pre>
  
  <p>But nothing seems to be working.</p>
  
  <p>Here is a sample of one of the simple operation functions I'm using:</p>
  
  <pre><code>void Add(){
      float x = 0, y = 0, z = 0;
      cout &lt;&lt; "Please enter two numbers you wish "
           &lt;&lt; "to add separated by a white space:" &lt;&lt; endl; 
      cin &gt;&gt; x &gt;&gt; y;
      z = x+y;
      cout &lt;&lt; x &lt;&lt; " + " &lt;&lt; y &lt;&lt; " = " &lt;&lt; z &lt;&lt; "." &lt;&lt; endl;
      return;
  }
  </code></pre>