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

#include <cassert>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

//??¨????
int calc(vector<string> vec) {
  vector<int> stack;

  for (int i = 0; i < vec.size(); i++) {
    //   cout << "i="<< i << "::" << vec[i] << endl;

    int res = 0;
    int sw = vec[i][0];
    switch (sw) {
    case '+': {
      int num1 = stack.back();
      stack.pop_back();
      int num2 = stack.back();
      stack.pop_back();
      res = num1 + num2;
      stack.push_back(res);
      //   cout << num1 << "+"<< num2 << "=" << res << endl;
      break;
    }
    case '-': {
      int num1 = stack.back();
      stack.pop_back();
      int num2 = stack.back();
      stack.pop_back();
      //   cout << "num1="<< num1 << endl;
      //   cout << "num2="<< num2 << endl;
      res = num2 - num1;
      stack.push_back(res);
      break;
    }
    case '*': {
      int num1 = stack.back();
      stack.pop_back();
      int num2 = stack.back();
      stack.pop_back();
      res = num1 * num2;
      stack.push_back(res);
      break;
    }
    case '/': {
      int num1 = stack.back();
      stack.pop_back();
      int num2 = stack.back();
      stack.pop_back();
      // 0??????
      assert(num1 != 0);
      res = num2 / num1;
      stack.push_back(res);
      break;
    }
    default: {
      stack.push_back(stoi(vec[i]));
      // res =  stoi(stack[i]);
    }
    }
    // cout <<"when i=" <<i << ";switch res::" << res << endl;
  }
  int ans = 0;
  ans = stack[0];
  return ans;
}

int main() {
  // input
  vector<string> vec;
  while (1) {
    string str;
    cin >> str;
    if (cin.eof()) {
      break;
    } else {
      vec.push_back(str);
    }
  }

  // calc
  int result = 0;
  result = calc(vec);

  // output
  cout << result << endl;

  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::invalid_argument'
  what():  stoi


Fixed submission:

#include <cassert>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

//??¨????
int calc(vector<string> vec) {
  vector<int> stack;

  for (int i = 0; i < vec.size(); i++) {
    //   cout << "i="<< i << "::" << vec[i] << endl;

    int res = 0;
    int sw = vec[i][0];
    switch (sw) {
    case '+': {
      int num1 = stack.back();
      stack.pop_back();
      int num2 = stack.back();
      stack.pop_back();
      res = num1 + num2;
      stack.push_back(res);
      //   cout << num1 << "+"<< num2 << "=" << res << endl;
      break;
    }
    case '-': {
      int num1 = stack.back();
      stack.pop_back();
      int num2 = stack.back();
      stack.pop_back();
      //   cout << "num1="<< num1 << endl;
      //   cout << "num2="<< num2 << endl;
      res = num2 - num1;
      stack.push_back(res);
      break;
    }
    case '*': {
      int num1 = stack.back();
      stack.pop_back();
      int num2 = stack.back();
      stack.pop_back();
      res = num1 * num2;
      stack.push_back(res);
      break;
    }
    case '/': {
      int num1 = stack.back();
      stack.pop_back();
      int num2 = stack.back();
      stack.pop_back();
      // 0??????
      assert(num1 != 0);
      res = num2 / num1;
      stack.push_back(res);
      break;
    }
    default: {
      stack.push_back(stoi(vec[i]));
      // res =  stoi(stack[i]);
    }
    }
    // cout <<"when i=" <<i << ";switch res::" << res << endl;
  }
  int ans = 0;
  ans = stack[0];
  return ans;
}

int main() {
  // input
  vector<string> vec;
  while (1) {
    string str;
    cin >> str;
    vec.push_back(str);
    if (cin.eof()) {
      break;
    }
  }

  // calc
  int result = 0;
  result = calc(vec);

  // output
  cout << result << endl;

  return 0;
}