title: How do I "reset" a function in C++?
body: <pre><code>    #include &lt;iostream&gt;
  
  double getUserInput()
  {
      std::cout &lt;&lt; "Input a number: ";
      double value;
      std::cin &gt;&gt; value;
      return value;
  }
  
  int getMathOp()
  {
      std::cout &lt;&lt; "Input one of the following math operators: +, -, * or / : ";
      char op;
      std::cin &gt;&gt; op;
      return op;
  }
  
  int printReuslt(double x, char op, double y)
  {
      if (op == '+')
          std::cout &lt;&lt; x &lt;&lt; " + " &lt;&lt; y &lt;&lt; " is " &lt;&lt; x + y &lt;&lt; '\n';
      else if (op == '-')
          std::cout &lt;&lt; x &lt;&lt; " - " &lt;&lt; y &lt;&lt; " is " &lt;&lt; x - y &lt;&lt; '\n';
      else if (op == '*')
          std::cout &lt;&lt; x &lt;&lt; " * " &lt;&lt; y &lt;&lt; " is " &lt;&lt; x * y &lt;&lt; '\n';
      else if (op == '/')
          std::cout &lt;&lt; x &lt;&lt; " / " &lt;&lt; y &lt;&lt; " is " &lt;&lt; x / y &lt;&lt; '\n';
      else 
          std::cout &lt;&lt; op &lt;&lt; " is not a valid operator. An error has occurred";
  }
  
  int main()
  {
      double x = getUserInput();
      double y = getUserInput();
  
      char op = getMathOp();
  
      printReuslt(x, op, y);
  
      return 0;
  }
  </code></pre>
  
  <p>I'm building a very simple calculator.</p>
  
  <ol>
  <li>getUserInput() - Takes a value (a number) from the user and
  stores it as a double variable called 'value'</li>
  <li>getMathOp() - Takes a value (a mathematical operator: +, -, * or
  /) from the user and stores it as a char variable called 'op'</li>
  <li>printResult() - Calculates the result
  for each instance of x op y (which are: x + y, x - y, x * y, x / y).
  It then prints that result.</li>
  <li>main() - Pulls each function together in the correct sequence.</li>
  </ol>
  
  <p>I want to add two additional pieces of functionality that do the following:</p>
  
  <ol>
  <li>If the user inputs a value other than a double, the programme will
  output a message that tells the user "value is not a number" and
  will make the user enter a value that is a double so the programme
  can continue.   </li>
  <li>If the user inputs a mathematical operator that is
  not in the message "+, -, * or /", the programme will output a
  message that tells the user "operator is not a valid operator" and
  will make the user enter a specified operator so the programme can
  continue.</li>
  </ol>
  
  <p>For the first piece of functionality, I've envisioned it by simply "resetting" the function to its first line - making the user go through the step of inputting in a number again.
  I would achieve this by having an 'if' statement that takes the input value to be something that is not a double which would execute a message saying "value is not a number". The function will then reset to the first line and ignore the previous input.</p>
  
  <p>For the second piece of functionality, I've made somewhat of a start by including an 'else' statement in 'printResult' that takes the input value to be something that is not specified as a math operator which executes a message "operator is not a valid operator. An error has occurred". Following this, the programme ends.
  I want that message to output and then reset the 'printResult' function to its first line (ignoring previous input) rather than ending the programme after the message output.</p>
  
  <p>My question is how  would I do what I am intending (resting a function to its first line, ignoring the previous input, so the user can input a new value) and what is the most optimal way to implement this functionality into my code?
  Also, any improvements to the intended functionality I want to add would be appreciated.</p>
  
  <p>Thanks</p>