Mixed expressions, loops and Formatted output

Hello,

I need some general guidance with the following problem:

1) ask the user to input a mathematical expression in the following format:
NUMBER Operator NUMBER Operator NUMBER
Example: 17 + 15 - 3
Example: 2 * 3 - 4

I am having trouble figuring out how to output the answer of the users equation. Is there a function that includes all math operators (+,-,/,*)? Would i need to write each possible scenario using if statements?

Any help is greatly appreciated.
Is there a function that includes all math operators (+,-,/,*)?
No. Write your own. Four operators are not too many.

Would i need to write each possible scenario using if statements?
No. You might use switch. It's not that difficult:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int do_operation(char op, int num1, int  num2)
{
  int result = 0;
  switch(op)
  {
  case '+':
  result = num1 + num2;
  break;
...
  }

  return result;
}
...
char op1, op2;
int num1, num2, num3;
...
cin >> num1 >> op1 >> num2 >> op2 >> num3;
cout << do_operation(op2, do_operation(op1, num1, num2), num3);
If that is all you doing (number operator number operator number) it won't be too long, and the code coder777 gave you works fine. Make sure you consider operator precedence too.
Topic archived. No new replies allowed.