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

#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  int i;
  int n;
  int tcard[100];
  int hcard[100];
  int ncard = 0, turn = 0;
  int tscore[100];
  int hscore[100];
  for (i = 0;; i++) {
    cin >> n;
    if (n == 0) {
      break;
    }
    ncard = 0;
    turn = 0;
    for (int a = 0; a < n; a++) {
      tcard[a] = 0;
      hcard[a] = 0;
    }
    tscore[i] = n;
    hscore[i] = n;

    for (int a = 0; a < n; a++) {
      cin >> tcard[a];
    }

    sort(tcard, tcard + n);

    int b = 0;
    int c = 1;

    for (int a = 0; a < n;) {
      if (c < tcard[b]) {
        hcard[a] = c;
        a++;
        c++;
      } else {
        b++;
        c++;
      }
    }

    for (;;) {
      if (turn == 0) {
        for (int b = 0; b <= n; b++) {
          if (b == n) {
            ncard = 0;
            turn = 1;
          } else if (ncard < tcard[b]) {
            ncard = tcard[b];
            tcard[b] = -1;
            hscore[i]--;
            turn = 1;
            break;
          }
        }
      }

      if (hscore[i] == 0 || tscore[i] == 0) {
        break;
      }

      if (turn == 1) {
        for (int b = 0; b <= n; b++) {
          if (b == n) {
            ncard = 0;
            turn = 0;
          } else if (ncard < hcard[b]) {
            ncard = hcard[b];
            hcard[b] = -1;
            tscore[i]--;
            turn = 0;
            break;
          }
        }
      }
      if (hscore[i] == 0 || tscore[i] == 0) {
        break;
      }
    }
  }

  for (int a = 0; a < i; a++) {
    cout << tscore[a] << endl;
    cout << hscore[a] << endl;
  }

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

int i;
int n;
int tcard[100];
int hcard[100];
int ncard = 0, turn = 0;
int tscore[100];
int hscore[100];

int main() {
  for (i = 0;; i++) {
    cin >> n;
    if (n == 0) {
      break;
    }
    ncard = 0;
    turn = 0;
    for (int a = 0; a < n; a++) {
      tcard[a] = 0;
      hcard[a] = 0;
    }
    tscore[i] = n;
    hscore[i] = n;

    for (int a = 0; a < n; a++) {
      cin >> tcard[a];
    }

    sort(tcard, tcard + n);

    int b = 0;
    int c = 1;

    for (int a = 0; a < n;) {
      if (c < tcard[b]) {
        hcard[a] = c;
        a++;
        c++;
      } else {
        b++;
        c++;
      }
    }

    for (;;) {
      if (turn == 0) {
        for (int b = 0; b <= n; b++) {
          if (b == n) {
            ncard = 0;
            turn = 1;
          }
          if (ncard < tcard[b]) {
            ncard = tcard[b];
            tcard[b] = -1;
            hscore[i]--;
            turn = 1;
            break;
          }
        }
      }

      if (hscore[i] == 0 || tscore[i] == 0) {
        break;
      }

      if (turn == 1) {
        for (int b = 0; b <= n; b++) {
          if (b == n) {
            ncard = 0;
            turn = 0;
          }
          if (ncard < hcard[b]) {
            ncard = hcard[b];
            hcard[b] = -1;
            tscore[i]--;
            turn = 0;
            break;
          }
        }
      }
      if (hscore[i] == 0 || tscore[i] == 0) {
        break;
      }
    }
  }

  for (int a = 0; a < i; a++) {
    cout << tscore[a] << endl;
    cout << hscore[a] << endl;
  }

  return 0;
}