Help with simple calculator program

This simple calculator program currently exectues by asking a user to choose an operation. It's followed by asking which 2 numbers they'd like to perform the operation on. How can I cange it so that I can just enter one equation (in the form or 4 *5 etc)?

#include<iostream>
using namespace std;


void getNumbers(int& op1, int&op2);
int addition(int op1, int op2);
double division(int op1, int op2);
int subtraction(int op1, int op2);
int multiplication(int op1, int op2);
bool parseInput( );

int main ()
{

parseInput();
}

// Enter values for equation
void getNumbers(int& op1, int&op2)
{
cout<<"Please enter your numbers ";
cin>>op1 >>op2;
}

// Perform addition
int addition(int op1, int op2)
{
return op1+op2;

}

// Perform division
double division(int op1, int op2)
{
if (op2 == 0){
cout << "Invalid operation. Cannot divide by 0";
}
else
return op1/op2;
}

//Perform subtraction
int subtraction(int op1, int op2)
{
return op1 - op2;
}

//Perform multiplication
int multiplication(int op1, int op2)
{
return op1 * op2;
}

bool parseInput( )
{

int op1, op2;
double sum;
char op;

do
{
cout << "Enter the sign of the operation you'd like to perform or press @ to quit the program ";
cin>> op;
cout<<endl;


switch (op)
{
case '+' :
getNumbers(op1, op2);
sum=addition(op1, op2);
cout<<op1 <<" + " <<op2 <<" = " <<sum <<"\n\n";
break;

case '/' :
getNumbers(op1, op2);
sum=division(op1, op2);
cout<<op1 <<" / " <<op2 <<" = " <<sum <<"\n\n";
break;

case '-' :
getNumbers(op1, op2);
sum=subtraction(op1, op2);
cout<<op1 <<" - " <<op2 <<" = " <<sum <<"\n\n";
break;

case '*' :
getNumbers(op1, op2);
sum=multiplication(op1, op2);
cout<<op1 <<" * " <<op2 <<" = " <<sum <<"\n\n";
break;



}
} while (op != '@');
}
closed account (48T7M4Gy)
To do what you want you have to parse the input string which means:
1. input the expression as a string, eg input = "2 + 3"
2. break the string down to the 3 tokens, "2", "+" and "3"
3. process the tokens by converting first and last to numbers, then performing the relevant operation
4. output the result
Topic archived. No new replies allowed.