Without further ado, I would use a structure with int and char fields, write to the stack right away and calculate.
Something like this (written on the knee, suboptimally and without error handling, to clarify the idea). To store in an array of strings, I believe, is inappropriate. Stack is a natural choice for reverse polish recording ...
struct Item { int i = 0; char c = 0; }; int main() { string input; getline(cin,input); stack<Item> st; for(int i = 0, end = input.length(); i < end; ++i) { switch(input[i]) { case ' ': break; case '+': case '-': case '*': case '/': { Item it; it.c = input[i]; st.push(it); // cout << "Push " << it.c << endl; break; } default: if (!isdigit(input[i])) { cerr << "Wrong symbol\n"; return 1; } Item it; char * pos; it.i = strtol(&input[i],&pos,10); i += pos-&input[i]; st.push(it); // cout << "Push " << it.i << endl; } } while(!st.empty()) { int i1 = st.top().i; st.pop(); if (st.empty()) { cout << "Result: " << i1 << endl; break; } int i2 = st.top().i; st.pop(); char op = st.top().c; st.pop(); switch(op) { case '+': i1 += i2; break; case '-': i1 -= i2; break; case '*': i1 *= i2; break; case '/': i1 /= i2; break; default: cerr << "Wrong expression\n"; return 1; } Item it; it.i = i1; st.push(it); } return 0; }
PS Yes, here the numbers are considered positive; just did not bother negative, so as not to complicate.