title: How do I accept either a string or a double as input? C++
body: <p>okay. So I wrote a formula calculator a while ago that does many different functions. I opened it today and noticed that every time I accessed a certain function and then finished that function, the program went back to the main menu. Of course this is fine and I programmed it to do this, but I got annoyed when I accessed the calculator function (simple math) and I finished an equation, I couldn't do another right away. I want to be able to stay in a certain function until I press 'q', and <em>then</em> it will go back to the main menu. </p>
  
  <p>The real problem, is that my function only accepts doubles, so if I put in a string ('q'), the program will just crash. I need a way to have the user enter either a string <em>or</em> a double so I can check if it's a 'q' and the user wants to quit.</p>
  
  <p>I want to eventually do this with all of my functions, but here's just the "calc" function (the simplest one):</p>
  
  <pre><code>    int calculation()
  {
    double x=0, y=0, answer=0;
    char o;//operator variable
    cout&lt;&lt;"calculation: ";
    cin&gt;&gt;x&gt;&gt;o&gt;&gt;y; //I just don't know what to use here so that the user can enter a  
    cin.ignore(); //double or a string.
    if (o=='+') answer=x+y;
    else if (o=='-') answer=x-y;
    else if (o=='*') answer=x*y;
    else if (o=='/') answer=x/y;
    else if (o=='^') answer= pow(x, y);
    else if (o=='%') {
          answer= (100/x)*y; 
          cout&lt;&lt;"answer= "&lt;&lt;answer&lt;&lt;"%";
    }
    if (o!='%') cout&lt;&lt;"answer= "&lt;&lt;answer;
    cin.get();
    return 0;
  }
  </code></pre>
  
  <p>I need the function to keep repeating until the user enters a single "q".
  Sorry for all the words.</p>