Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9020427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:00:50+00:00 2026-06-16T05:00:50+00:00

okay. So I wrote a formula calculator a while ago that does many different

  • 0

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 then it will go back to the main menu.

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 or a double so I can check if it’s a ‘q’ and the user wants to quit.

I want to eventually do this with all of my functions, but here’s just the “calc” function (the simplest one):

    int calculation()
{
  double x=0, y=0, answer=0;
  char o;//operator variable
  cout<<"calculation: ";
  cin>>x>>o>>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<<"answer= "<<answer<<"%";
  }
  if (o!='%') cout<<"answer= "<<answer;
  cin.get();
  return 0;
}

I need the function to keep repeating until the user enters a single “q”.
Sorry for all the words.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T05:00:51+00:00Added an answer on June 16, 2026 at 5:00 am

    Solve it in two steps:

    1. Write a loop that accepts any string and only quits when you enter a ‘q’.
    2. Pass that string to a new function that will only perform a calculation if the string can be decomposed into the two operands and an operator (you can use stringstream for this).

    After that, you can customize your functions however you want. One possibility would be mapping strings for operators to callbacks that take two operands and print a result. You would then pass that mapping in to your calculation function so that the number of operators you support can increase with ease.

    Here’s a very rough example that only works for addition, but demonstrates the concept.

    #include <iostream>
    #include <cmath>
    #include <sstream>
    #include <map>
    using namespace std;
    
    typedef double (*p_fn)(double,double);
    
    double add(double x, double y)
    {
        return x + y;
    }
    
    typedef map<string,p_fn> operators;
    
    double execute( const operators &op, double x, double y, const string& o )
    {
        operators::const_iterator i = op.find(o);
        if( i != op.end())
        {
            p_fn f = i->second;
            double const result = f(x,y);
            return result;
        }
        cout<<"unknown operator\n";
        return 0;
    }
    
    bool get_data( double& x, double&y, string& o )
    {
        string s1,s2,s3;
        cin>>s1;
        if(s1=="q")
            return false;
        cin>>s2>>s3;
        stringstream sstr;
        sstr<<s1<<" "<<s2<<" "<<s3;
        sstr>>x>>o>>y;
        stringstream sstr2;
        sstr2<<x<<" "<<o<<" "<<y;
        return sstr.str() == sstr2.str();
    }
    
    double calculation2( const operators& op )
    {
        double x,y;
        string o;
        while(get_data(x,y,o))
            cout<<execute(op, x, y, o)<<"\n";
        return 0;
    }
    
    int main(int argc, char* argv[])
    {
        operators o;
        o["+"]=add;
        calculation2(o);
        return 0;
    }
    

    This example uses pointers to functions to map the string “+” to the function add(x,y). It also uses stringstreams to perform very basic input validation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, so I wrote this just under a decade ago: http://meyerweb.com/eric/tools/color-blend/ I'm pretty sure
Okay, up until now, I thought that functions defined in header files are treated
I have used IB to do that,and everything is okay,but when I wrote code
Okay, so i have this code i wrote: class Connection { public static StreamWriter
Okay, so I was teaching my girlfriend some c++, and she wrote a program
Okay, next PHPExcel question. I have an HTML form that users fill out and
I wrote some code: output = File.open(text_file).collect.reverse.join(<BR>) It seems to work okay on 1.8.7
i wrote the following section below. when debugging, i see that i enter the
Update: I wrote a working script that finishes this job in a reasonable length
Okay, I have a page on a Drupal install that has multiple divs. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.