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

#include <algorithm> // require sort next_permutation count etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>  // require printf
#include <cstdlib> // require abs
#include <cstring> // require memset
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>

using namespace std;

string itos(int n) {
  stringstream ss;
  ss << n;

  return ss.str();
}

int main() {
  //	cut here before submit
  //	freopen ("testcase.citation", "r", stdin );
  int n;

  while (cin >> n && n) {

    vector<int> page(n + 1, 0);

    for (int i = 0; i < n; ++i) {
      cin >> page[i];
    } // end for
    page[n] = 1000000;

    vector<string> res;

    bool cont = false;
    string str = "";

    for (int i = 0; i < n; ++i) {
      if (!cont) {
        str = itos(page[i]);
      } // end if

      if (page[i + 1] == page[i] + 1) {
        if (!cont) {
          cont = true;
        } // end if
        continue;
      } else {
        if (!cont) {
          res.push_back(str);
        } else {
          str += '-';
          str += itos(page[i]);
          res.push_back(str);
          cont = false;
        } // end if
      }   // end if
    }     // end for

    int size = res.size();

    for (int i = 0; i < size; ++i) {
      cout << res[i];
      if (i != size - 1) {
        cout << ' ';
      } // end if
    }   // end for
    cout << endl;
  } // end loop

  return 0;
}

Error: 0

Fixed submission:

#include <cstring>
#include <iostream>
#include <sstream>
#include <vector>
// #include <fstream>

using namespace std;
const int M = -10000;

string itos(int n) {
  stringstream ss;
  ss << n;

  return ss.str();
}

int main() {
  //	cut here before submit
  //	freopen ("testcase.cf", "r", stdin );
  int n;

  while (cin >> n && n) {

    bool page[50 + 5];
    memset(page, false, sizeof(page));
    int i;

    for (i = 0; i < n; ++i) {
      int p;
      cin >> p;
      page[p] = true;
    } // end for

    bool found = false;
    int start = M;
    vector<string> res;

    for (i = 1; i <= 51; ++i) {
      if (page[i] && !found) {
        start = i;
        found = true;
      } else if ((page[i] && found) || (!page[i] && !found)) {
        continue;
      } else { // if (!page[i] && found )
        if (i == start + 1) {
          res.push_back(itos(start));
        } else if (i >= start + 2) {
          string str = "";
          str += itos(start);
          str += '-';
          str += itos(i - 1);
          res.push_back(str);
        } // end if
        start = M;
        found = false;
      } // end if
    }   // end if

    if (!res.empty()) {
      for (i = 0; i < res.size(); ++i) {
        cout << res[i];
        if (i != res.size() - 1) {
          cout << ' ';
        } // end if
      }   // end for
      cout << endl;
    } // end if
  }   // end loop

  return 0;
}