I'm new to C++ and need to write a program that reads and calculates math equations from a char string. My question: is there a way to convert a char math symbol such as '+' into a + the compiler can use in an equation?
Right now I'm using a bunch of If statements to handle this, but would like to know if there's a way to work the char sign directly into the equation.
i.e.
char num1 = '5';
char sign = '+';
char num2 = '2';
if (sign == '+')
{
int sum = (num1 - '0') + (num2 - '0');
// want to do somthing like: int sum = (num1 - '0') sign (num2 - '0');
}
if (sign == '-')
.... etc
Thanks.
Right now I'm using a bunch of If statements to handle this, but would like to know if there's a way to work the char sign directly into the equation.
i.e.
char num1 = '5';
char sign = '+';
char num2 = '2';
if (sign == '+')
{
int sum = (num1 - '0') + (num2 - '0');
// want to do somthing like: int sum = (num1 - '0') sign (num2 - '0');
}
if (sign == '-')
.... etc
Thanks.
Comment