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

#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
int n;

int check(int num, int start, string w[40]) {
  int count = start;
  bool flag = true;
  while (flag) {
    if (count >= n) {
      flag = false;
      break;
    } else if (num - w[count].size() == 0) {
      count++;
      break;
    } else if (num - w[count].size() > 0) {
      num -= w[count].size();
      count++;
      continue;
    } else {
      flag = false;
      break;
    }
  }
  if (flag)
    return count;
  else
    return -1;
}

int main() {
  string w[40];
  while (true) {
    cin >> n;
    // cout << "n="<<n << endl;
    if (n == 0)
      break;

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

    for (int i = 0; i < n - 4; i++) {
      int point = i;

      point = check(5, i, w);
      if (point == -1)
        continue;
      point = check(7, point, w);
      if (point == -1)
        continue;
      point = check(5, point, w);
      if (point == -1)
        continue;
      point = check(7, point, w);
      if (point == -1)
        continue;
      point = check(7, point, w);
      if (point != -1) {
        cout << i + 1 << endl;
        break;
      }
    }
  }
  return 0;
}


Error: 0

Fixed submission:

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, a, b, c, d, e, j;
  string w[40];
  while (true) {
    cin >> n;
    if (n == 0)
      break;

    for (int i = 0; i < n; i++) {
      cin >> w[i];
    }
    // cout << "n="<<n << endl;
    for (int i = 0; i < n - 4; i++) {
      // start w[i] 5 7 5 7 7
      // cout << i << "=i" <<endl;
      a = 5;
      b = 7;
      c = 5;
      d = 7;
      e = 7;

      int j = i;
      bool flag = true;
      while (flag) { // 5

        if (a - w[j].size() == 0) {
          j++;
          break;
        } else if (j >= n) {
          flag = false;
        } else if (a - w[j].size() > 0) {
          a -= w[j].size();
          j++;
        } else {
          flag = false;
        }
      }
      // cout << j << endl;
      while (flag) { // 7

        if (b - w[j].size() == 0) {
          j++;
          break;
        } else if (j >= n) {
          flag = false;
        } else if (b - w[j].size() > 0) {
          b -= w[j].size();
          j++;
        } else {
          flag = false;
        }
      }
      // cout << j << endl;
      while (flag) { // 5

        if (c - w[j].size() == 0) {
          j++;
          break;
        } else if (j >= n) {
          flag = false;
        } else if (c - w[j].size() > 0) {
          c -= w[j].size();
          j++;
        } else {
          flag = false;
        }
      }
      // cout << j << endl;
      while (flag) { // 7

        if (d - w[j].size() == 0) {
          j++;
          break;
        } else if (j >= n) {
          flag = false;
        } else if (d - w[j].size() > 0) {
          d -= w[j].size();
          j++;
        } else {
          flag = false;
        }
      }
      // cout <<  j << endl;
      while (flag) { // 7

        if (e - w[j].size() == 0) {
          j++;
          break;
        } else if (j >= n) {
          flag = false;
        } else if (e - w[j].size() > 0) {
          e -= w[j].size();
          j++;
        } else {
          flag = false;
        }
      }
      if (flag) {
        cout << i + 1 << endl;
        break;
      }
    }
  }

  return 0;
}