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

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

string S;
size_t cur = 0;
int parse();
int expression();

int digit() {
  // assert(isdigit(S[cur]));
  int n = S[cur] - '0';
  cur = cur + 1;
  // cout << n <<endl;
  return n;
}

int number() {
  int n = digit();
  // cout << n << endl;
  while (cur < S.size() && isdigit(S[cur]))
    n = n * 10 + digit();
  // cout << n<<endl;
  return n;
}

int factor() {
  if (S[cur] != '(') {
    return number();
  }
  cur += 1;
  int n = expression();
  assert(S[cur] == ')');
  cur += 1;
  return n;
}

int term() {
  int a = factor();
  while (cur < S.size() && (S[cur] == '*' || S[cur] == '/')) {
    char op = S[cur++];
    int b = factor();
    if (op == '*')
      a *= b;
    else
      a /= b;
  }
  return a;
}

int expression() {
  int a = term();
  while (cur < S.size() && (S[cur] == '+' || S[cur] == '-')) {
    char op = S[cur++];
    int b = term();
    if (op == '+')
      a += b;
    else
      a -= b;
  }
  return a;
  // int sum = number();
  // while(S[cur] == '+'|| S[cur] == '-'||S[cur]=='*'||S[sur]=='/'){
  //   char op = S[cur];
  //   cur += 1;
  //   int b = number();
  //   if(op=='+'){

  //   }

  // }

  // int a = number();
  // char op = S[cur];
  // cur += 1;
  // int b = number();
  // assert(op == '+');
  // return a + b;
}

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; ++i) {
    cur = 0;
    cin >> S;
    S.resize(S.size() - 1);
    cout << expression() << endl;
  }
}
// int parse(){return expression();}
// int main(){
//   int a = parse();
//   // assert(a==15);
//   cout << a << endl;
//   assert(cur == S.size());
// }

Error: -6
dd4ab3d7-57cb-482e-99f3-b5ffaea13d11.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p00109/C++/s951061656.cpp:15: int digit(): Assertion `isdigit(S[cur])' failed.


Fixed submission:

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

string S;
size_t cur = 0;
int parse();
int expression();

int digit() {
  assert(isdigit(S[cur]));
  int n = S[cur] - '0';
  cur = cur + 1;
  // cout << n <<endl;
  return n;
}

int number() {
  int n = digit();
  // cout << n << endl;
  while (cur < S.size() && isdigit(S[cur]))
    n = n * 10 + digit();
  // cout << n<<endl;
  return n;
}

int factor() {
  if (S[cur] != '(') {
    return number();
  }
  cur += 1;
  int n = expression();
  assert(S[cur] == ')');
  cur += 1;
  return n;
}

int term() {
  int a = factor();
  while (cur < S.size() && (S[cur] == '*' || S[cur] == '/')) {
    char op = S[cur++];
    int b = number();
    if (op == '*')
      a *= b;
    else
      a /= b;
  }
  return a;
}

int expression() {
  int a;
  a = term();
  while (cur < S.size() && (S[cur] == '+' || S[cur] == '-')) {
    char op = S[cur++];
    int b = term();
    if (op == '+')
      a += b;
    else
      a -= b;
  }
  return a;
  // int sum = number();
  // while(S[cur] == '+'|| S[cur] == '-'||S[cur]=='*'||S[sur]=='/'){
  //   char op = S[cur];
  //   cur += 1;
  //   int b = number();
  //   if(op=='+'){

  //   }

  // }

  // int a = number();
  // char op = S[cur];
  // cur += 1;
  // int b = number();
  // assert(op == '+');
  // return a + b;
}

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; ++i) {
    cur = 0;
    cin >> S;
    S.resize(S.size() - 1);
    cout << expression() << endl;
  }
}
// int parse(){return expression();}
// int main(){
//   int a = parse();
//   // assert(a==15);
//   cout << a << endl;
//   assert(cur == S.size());
// }