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

/**
 * ????????? runtime error ?????????
 */

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, start, end) for (int i = (start); i < (end); ++i)
#define REP(i, n) FOR(i, 0, n)

const int OP_PLUS = -1;
const int OP_MINUS = -2;
const int OP_MUL = -3;

vector<int> read_expr() {
  vector<int> expr;

  int c;
  string buf;
  while ((c = getchar()) != EOF) {
    if (isspace(c)) {
      if (!buf.empty()) {
        expr.push_back(atoi(buf.c_str()));
        buf.clear();
      }
      if (c == '\\n')
        break;
    } else if (isdigit(c)) {
      buf += static_cast<char>(c);
    } else if (c == '+') {
      expr.push_back(OP_PLUS);
    } else if (c == '-') {
      expr.push_back(OP_MINUS);
    } else if (c == '*') {
      expr.push_back(OP_MUL);
    }
  }

  return expr;
}

int interpret(const vector<int> &expr) {
  stack<int> s;

  for (int sym : expr) {
    if (sym >= 0) {
      s.push(sym);
    } else if (sym == OP_PLUS) {
      int rhs = s.top();
      s.pop();
      int lhs = s.top();
      s.pop();
      s.push(lhs + rhs);
    } else if (sym == OP_MINUS) {
      int rhs = s.top();
      s.pop();
      int lhs = s.top();
      s.pop();
      s.push(lhs - rhs);
    } else if (sym == OP_MUL) {
      int rhs = s.top();
      s.pop();
      int lhs = s.top();
      s.pop();
      s.push(lhs * rhs);
    }
  }

  return s.top();
}

int main() {
  vector<int> expr = read_expr();
#if 0
    REP(i, expr.size()) {
        printf("%d ", expr[i]);
    }
#endif

  int result = interpret(expr);
  printf("%d\\n", result);
  fflush(stdout);

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, start, end) for (int i = (start); i < (end); ++i)
#define REP(i, n) FOR(i, 0, n)

const int OP_PLUS = -1;
const int OP_MINUS = -2;
const int OP_MUL = -3;

vector<int> read_expr() {
  vector<int> expr;

  int c;
  string buf;
  while ((c = getchar()) != EOF) {
    if (isspace(c)) {
      if (!buf.empty()) {
        expr.push_back(atoi(buf.c_str()));
        buf.clear();
      }
      if (c == '\\n')
        break;
    } else if (isdigit(c)) {
      buf += static_cast<char>(c);
    } else if (c == '+') {
      expr.push_back(OP_PLUS);
    } else if (c == '-') {
      expr.push_back(OP_MINUS);
    } else if (c == '*') {
      expr.push_back(OP_MUL);
    }
  }

  return expr;
}

int interpret(const vector<int> &expr) {
  stack<int> s;

  for (int sym : expr) {
    if (sym >= 0) {
      s.push(sym);
    } else if (sym == OP_PLUS) {
      int rhs = s.top();
      s.pop();
      int lhs = s.top();
      s.pop();
      s.push(lhs + rhs);
    } else if (sym == OP_MINUS) {
      int rhs = s.top();
      s.pop();
      int lhs = s.top();
      s.pop();
      s.push(lhs - rhs);
    } else if (sym == OP_MUL) {
      int rhs = s.top();
      s.pop();
      int lhs = s.top();
      s.pop();
      s.push(lhs * rhs);
    }
  }

  return s.top();
}

int main() {
  vector<int> expr = read_expr();
#if 0
    REP(i, expr.size()) {
        printf("%d ", expr[i]);
    }
#endif

  int result = interpret(expr);
  printf("%d\\n", result);
  fflush(stdout);

  return 0;
}