title: How can I avoid bad input from a user?
body: <p>I am a very newbie programmer, so I don't really know much about writing code to protect the application.. Basically, I created a basicMath.h file and created a do while loop to make a very basic console calculator (only two floats are passed through the functions).  I use a series of if and else if statements to determine what the users wants to do. (1.add, 2.subtract, 3.multiply, 4.divide) I used a else { cout &lt;&lt; "invalid input" &lt;&lt; endl;} to protect against any other values, but then I tried to actually write a letter, and the program entered a infinite loop.  Is there anyway to protect against users who accidentally hit a character instead of a number? </p>
  
  <pre><code> `#include &lt;iostream&gt;
    #include "basicMath.h"
  
    using namespace std;
    char tryAgain = 'y';
    float numOne = 0, numTwo = 0;
    int options = 0;
    int main()
    {
     cout &lt;&lt; "welcome to my calculator program." &lt;&lt; endl;
   cout &lt;&lt; "This will be a basic calculator." &lt;&lt; endl;
   do{
      cout &lt;&lt; "What would you like to do?" &lt;&lt; endl;
      cout &lt;&lt; "1. Addition." &lt;&lt; endl;
      cout &lt;&lt; "2. Subtraction." &lt;&lt; endl;
      cout &lt;&lt; "3. Multiplication" &lt;&lt; endl;
      cout &lt;&lt; "4. Division." &lt;&lt; endl;
      cin &gt;&gt; options;
      if (options == 1){
          cout &lt;&lt; "Enter your first number." &lt;&lt; endl;
          cin &gt;&gt; numOne;
          cout &lt;&lt; "Enter your second number." &lt;&lt; endl;
          cin &gt;&gt; numTwo;
          cout &lt;&lt; numOne &lt;&lt; " + " &lt;&lt; numTwo &lt;&lt; " = " &lt;&lt; add(numOne, numTwo) &lt;&lt; endl;
      }
      else if (options == 2){
          cout &lt;&lt; "Enter your first number." &lt;&lt; endl;
          cin &gt;&gt; numOne;
          cout &lt;&lt; "Enter your second number." &lt;&lt; endl;
          cin &gt;&gt; numTwo;
          cout &lt;&lt; numOne &lt;&lt; " - " &lt;&lt; numTwo &lt;&lt; " = " &lt;&lt; subtract(numOne, numTwo) &lt;&lt; endl;
      }
      else if (options == 3){
          cout &lt;&lt; "Enter your first number." &lt;&lt; endl;
          cin &gt;&gt; numOne;
          cout &lt;&lt; "Enter your second number." &lt;&lt; endl;
          cin &gt;&gt; numTwo;
          cout &lt;&lt; numOne &lt;&lt; " * " &lt;&lt; numTwo &lt;&lt; " = " &lt;&lt; multiply(numOne, numTwo) &lt;&lt; endl;
      }
      else if (options == 4){
          cout &lt;&lt; "Enter your first number." &lt;&lt; endl;
          cin &gt;&gt; numOne;
          cout &lt;&lt; "Enter your second number." &lt;&lt; endl;
          cin &gt;&gt; numTwo;
          cout &lt;&lt; numOne &lt;&lt; " / " &lt;&lt; numTwo &lt;&lt; " = " &lt;&lt; divide(numOne, numTwo) &lt;&lt; endl;
      }
      else {
          cout &lt;&lt; "Error, invalid option input." &lt;&lt; endl;
      }
      cout &lt;&lt; "Would you like to use this calculator again? (y/n)" &lt;&lt; endl;
      cin &gt;&gt; tryAgain;
  }while (tryAgain == 'y');
  cout &lt;&lt; "Thank you for using my basic calculator!" &lt;&lt; endl;
  return 0;
  }
   `
  </code></pre>