You are working with binary operators: one operator needs two operands. E.g. 2 + 3 --> [operand] [operator] [operand]
On the other hand, your code (incorrectly) asks for an operator after every operand (number). 2 + 3 +. Your code applies the first operator to the first operand (number) and the existing Sum variable (which is initialized to 0).
One solution would be the following:
- After having the first number, you set Sum := firstNumber.
- When the used needs to provide n pieces of numbers, then you ask for n-1 pieces of operators, not asking for any more operators after the nth number.
Also, for input, you have a problem: reading a single character messes up the input stream buffer. Try the following instead:
#include <cstdio> // for sscanf
// ...
cin >> buffer;
char operator1;
sscanf(buffer.c_str(), "%c", &operator1);