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

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
#define satoi(n) atoi(n.c_str())

string pls(string a, string b) {
  string ret;
  reverse(a.begin(), a.end());
  reverse(b.begin(), b.end());
  if (a.size() < b.size())
    swap(a, b);
  while (a.size() != b.size())
    b += '0';
  while (a.size() + 1 != ret.size())
    ret += '0';
  for (int i = 0; i < a.size(); i++) {
    ret[i] = a[i] + b[i] + ret[i] - '0' * 3;
    if (ret[i] > 9) {
      ret[i] = '0' + ret[i] % 10;
      ret[i + 1] = '1';
    } else {
      ret[i] = '0' + ret[i];
    }
  }

  reverse(ret.begin(), ret.end());
  if (~ret.find_first_not_of('0'))
    return ret.substr(ret.find_first_not_of('0'),
                      ret.size() - ret.find_first_not_of('0'));
  else
    return "0";
}
bool func(vector<string> f, int n) {
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < f[i].length(); j++) {
      if (f[i][j] == 'X') {
        f[i][j] = '0' + n;
      }
    }
  }
  return (pls(f[0], f[1]) == f[2]);
}

int main() {
  bool flag;
  int ret;
  vector<string> m;
  string s, t;
  while (cin >> s) {
    s += " ";
    m.clear();
    flag = false;
    ret = -1;

    for (int i = 0; i < s.length(); i++) {
      if (s[i] >= '0' && s[i] <= '9' || s[i] == 'X') {
        t += s[i];
      } else {
        if (t.size() >= 2 && t[0] == 'X')
          flag = true;
        m.push_back(t);
        t.clear();
      }
    }
    for (int i = (flag ? 1 : 0); i < 10; i++) {
      if (func(m, i))
        ret = i;
    }
    if (~ret)
      cout << ret << endl;
    else
      cout << "NA" << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
#define satoi(n) atoi(n.c_str())

string pls(string a, string b) {
  string ret;
  reverse(a.begin(), a.end());
  reverse(b.begin(), b.end());
  if (a.size() < b.size())
    swap(a, b);
  while (a.size() != b.size())
    b += '0';
  while (a.size() + 1 != ret.size())
    ret += '0';
  for (int i = 0; i < a.size(); i++) {
    ret[i] = a[i] + b[i] + ret[i] - '0' * 3;
    if (ret[i] > 9) {
      ret[i] = '0' + ret[i] % 10;
      ret[i + 1] = '1';
    } else {
      ret[i] = '0' + ret[i];
    }
  }
  reverse(ret.begin(), ret.end());
  return ret.substr(ret.find_first_not_of('0'),
                    ret.size() - ret.find_first_not_of('0'));
}
bool func(vector<string> f, int n) {
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < f[i].length(); j++) {
      if (f[i][j] == 'X') {
        f[i][j] = '0' + n;
      }
    }
  }
  if (pls(f[0], f[1]) == f[2])
    return true;
  else
    return false;
}

int main() {
  bool flag;
  int ret;
  vector<string> m;
  string s, t;
  while (cin >> s) {
    s += " ";
    m.clear();
    flag = false;
    ret = -1;

    for (int i = 0; i < s.length(); i++) {
      if (s[i] >= '0' && s[i] <= '9' || s[i] == 'X') {
        t += s[i];
      } else {
        if (t.size() >= 2 && t[0] == 'X')
          flag = true;
        m.push_back(t);
        t.clear();
      }
    }
    for (int i = (flag ? 1 : 0); i < 10; i++) {
      if (func(m, i))
        ret = i;
    }
    if (~ret)
      cout << ret << endl;
    else
      cout << "NA" << endl;
  }
}