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

#include <algorithm>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int main() {

  int n;
  cin >> n;

  while (n-- > 0) {
    vector<int> ans;
    vector<int> a;
    vector<int> b;

    string c, d;
    cin >> c >> d;

    for (int i = 0; i < (int)c.size(); ++i) {
      a.push_back(c[c.size() - 1 - i] - '0');
    }
    for (int i = 0; i < (int)d.size(); ++i) {
      b.push_back(d[d.size() - 1 - i] - '0');
    }

    int kuri = 0;
    while (!a.empty() || !b.empty() || kuri != 0) {
      int sum = kuri;
      kuri = 0;

      if (!a.empty()) {
        sum += a[0];
        a.erase(a.begin());
      }

      if (!b.empty()) {
        sum += b[0];
        b.erase(b.begin());
      }

      ans.push_back(sum % 10);
      kuri += sum / 10;
    }

    if (ans.size() > 80)
      puts("overflow");
    else {
      for (int i = 0; i < (int)ans.size(); ++i) {
        printf("%d", ans[ans.size() - 1 - i]);
      }
      puts("");
    }
  }
}

Error: WA

Fixed submission:

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;

  while (n-- > 0) {

    string str1;
    cin >> str1;

    string str2;
    cin >> str2;

    reverse(str1.begin(), str1.end());
    reverse(str2.begin(), str2.end());

    int check;
    if (str1.size() > str2.size()) {
      check = 1;
    } else if (str1.size() == str2.size()) {
      check = 2;
    } else {
      check = 3;
    }

    int ans[100];
    bool next = false;
    int short_ = min(str1.size(), str2.size());
    int long_ = max(str1.size(), str2.size());
    for (int i = 0; i < short_; ++i) {
      int num = 0;
      num += (str1[i] - '0') + (str2[i] - '0');
      if (next) {
        num++;
      }
      ans[i] = num;
      next = false;
      if (num >= 10) {
        num -= 10;
        ans[i] -= 10;
        next = true;
      }
      // cout << num << endl;
    }
    /*for(int i = 0; i < min(str1.size(), str2.size()); ++i){
            cout << ans[i];
    }
    cout << endl;*/

    if (check == 1) {
      for (int i = str2.size(); i < str1.size(); ++i) {
        int num = str1[i] - '0';
        if (next) {
          num++;
        }
        ans[i] = num;
        next = false;
        if (num >= 10) {
          num -= 10;
          ans[i] -= 10;
          next = true;
        }
      }
      if (next) {
        ans[str1.size()] = 1;
        long_++;
      }
    }

    if (check == 3) {
      for (int i = str1.size(); i < str2.size(); ++i) {
        int num = str2[i] - '0';
        if (next) {
          num++;
        }
        ans[i] = num;
        next = false;
        if (num >= 10) {
          num -= 10;
          ans[i] -= 10;
          next = true;
        }
      }
      if (next) {
        ans[str2.size()] = 1;
        long_++;
      }
    }

    for (int i = long_ - 1; i >= 0; --i) {
      cout << ans[i];
    } /*
     for(int i = 0; i < min(str1.size(), str2.size()) + 1; ++i){
             cout << ans[i];
     }*/

    cout << endl;
    // reverse(ans.begin(), ans.end());
    // cout << ans << endl;
  }

  return 0;
}