Problem p00109 (C++) — buggy submission, verdict: Runtime Error

#include <cctype>
#include <iostream>
#include <string>
using namespace std;

typedef string::const_iterator State;

int expression(State &);
int term(State &);
int number(State &);
int factor(State &);

int expression(State &begin) {
  int ret = term(begin);
  for (;;) {
    if (*begin == '+') {
      ++begin;
      ret += term(begin);
    } else if (*begin == '-') {
      ++begin;
      ret -= term(begin);
    } else
      break;
  }
  return ret;
}

int term(State &begin) {
  int ret = factor(begin);
  for (;;) {
    if (*begin == '*') {
      ++begin;
      ret *= factor(begin);
    } else if (*begin == '/') {
      ++begin;
      ret /= factor(begin);
    } else {
      break;
    }
  }
  return ret;
}

int factor(State &begin) {
  int ret;
  if (*begin == '(') {
    ++begin;
    ret = expression(begin);
    ++begin;
  } else {
    ret = number(begin);
  }
  return ret;
}

int number(State &begin) {
  int ret = 0;
  while (isdigit(*begin)) {
    ret *= 10;
    ret += *begin - '0';
    ++begin;
  }
  return ret;
}

int main() {
  int n;
  string q;
  cin >> n;
  cin.ignore();
  for (int i(0); i < n; ++i) {
    getline(cin, q);
    State begin = q.begin();
    cout << expression(begin) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n;
  string q, a, sop;
  stack<int> numstack;
  stack<string> opstack;
  map<string, int> oprank;
  vector<string> exq;
  oprank["/"] = oprank["*"] = 2;
  oprank["+"] = oprank["-"] = 1;
  oprank["("] = 0;
  cin >> n;
  for (int i(0); i < n; ++i) {
    cin >> q;
    exq.clear();
    a = "";
    for (int j(0); j < q.length(); ++j) {
      if (q[j] < '0' || '9' < q[j]) {
        if (a.length() > 0)
          exq.push_back(a);
        a = "";
        sop = string(1, q[j]);
        if (opstack.empty()) {
          opstack.push(sop);
          continue;
        }
        if (sop == ")") {
          while (opstack.top() != "(") {
            exq.push_back(opstack.top());
            opstack.pop();
          }
          opstack.pop();
        } else if (sop == "(")
          opstack.push(sop);
        else if (sop == "=") {
          while (!opstack.empty()) {
            exq.push_back(opstack.top());
            opstack.pop();
          }
        } else if (oprank[sop] <= oprank[opstack.top()]) {
          while (!opstack.empty() && oprank[sop] <= oprank[opstack.top()]) {
            exq.push_back(opstack.top());
            opstack.pop();
          }
          opstack.push(sop);
        } else
          opstack.push(sop);
      } else
        a += q[j];
    }
    for (string s : exq) {
      if (s[0] < '0' || '9' < s[0]) {
        int x, y;
        y = numstack.top();
        numstack.pop();
        x = numstack.top();
        numstack.pop();
        if (s == "+") {
          numstack.push(x + y);
        } else if (s == "-") {
          numstack.push(x - y);
        } else if (s == "*") {
          numstack.push(x * y);
        } else if (s == "/") {
          numstack.push(x / y);
        }
      } else
        numstack.push(stoi(s));
    }
    cout << numstack.top() << endl;
    numstack.pop();
  }
  return 0;
}