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

#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
typedef string::const_iterator State;
int len;
string inpro;
string in[50000];
string str;
vector<string> property;

void split(const string &str, char delim) {
  istringstream iss(str);
  string tmp;
  getline(iss, tmp, delim);
  while (getline(iss, tmp, delim))
    property.push_back(tmp);
  len = property.size();
}

string func(State &begin, int depth, int indent) {
  if (begin == str.end())
    return "no such property";
  int cnt = 0;
  while (*begin == ' ') {
    begin++;
    cnt++;
  }
  if (indent == -1)
    indent = cnt;
  else if (indent != cnt) {
    while (*begin != '\\n' && begin != str.end())
      begin++;
    if (cnt < indent || begin == str.end())
      return "no such property";
    return func(++begin, depth, indent);
  }
  string key = "";
  while (*begin != ':') {
    key += *begin;
    begin++;
  }
  begin++;
  if (key == property[depth]) {
    if (depth == len - 1) {
      if (*begin == '\\n')
        return "object";
      else {
        string res = "";
        begin++;
        while (*begin != '\\n') {
          res += *begin;
          begin++;
        }
        string ans = "string ";
        ans += '"';
        ans += res;
        ans += '"';
        return ans;
      }
    } else {
      if (*begin == '\\n') {
        return func(++begin, ++depth, -1);
      } else
        return "no such property";
    }
  } else {
    while (*begin != '\\n' && begin != str.end())
      begin++;
    if (begin == str.end())
      return "no such property";
    return func(++begin, depth, indent);
  }
}

int main(void) {
  getline(cin, inpro);
  split(inpro, '.');
  int cnt = 0;
  while (getline(cin, in[cnt]))
    cnt++;
  for (int i = 0; i < cnt; i++) {
    str += in[i];
    str += '\\n';
  }
  State begin = str.begin();
  cout << func(begin, 0, -1) << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
typedef string::const_iterator State;
int len;
string inpro;
string in[50];
string str;
vector<string> property;

void split(const string &str, char delim) {
  istringstream iss(str);
  string tmp;
  getline(iss, tmp, delim);
  while (getline(iss, tmp, delim))
    property.push_back(tmp);
  len = property.size();
}

string func(State &begin, int depth, int indent) {
  if (begin == str.end())
    return "no such property";
  int cnt = 0;
  while (*begin == ' ') {
    begin++;
    cnt++;
  }
  if (indent == -1)
    indent = cnt;
  else if (indent != cnt) {
    while (*begin != '\\n' && begin != str.end())
      begin++;
    if (cnt < indent || begin != str.end())
      return "no such property";
    return func(++begin, depth, indent);
  }
  string key = "";
  while (*begin != ':') {
    key += *begin;
    begin++;
  }
  begin++;
  if (key == property[depth]) {
    if (depth == len - 1) {
      if (*begin == '\\n')
        return "object";
      else {
        string res = "";
        begin++;
        while (*begin != '\\n') {
          res += *begin;
          begin++;
        }
        string ans = "string ";
        ans += '"';
        ans += res;
        ans += '"';
        return ans;
      }
    } else {
      if (*begin == '\\n') {
        return func(++begin, ++depth, -1);
      } else
        return "no such property";
    }
  } else {
    while (*begin != '\\n' && begin != str.end())
      begin++;
    if (begin == str.end())
      return "no such property";
    return func(++begin, depth, indent);
  }
}

int main(void) {
  getline(cin, inpro);
  split(inpro, '.');
  int cnt = 0;
  while (getline(cin, in[cnt]))
    cnt++;
  for (int i = 0; i < cnt; i++) {
    str += in[i];
    str += '\\n';
  }
  State begin = str.begin();
  cout << func(begin, 0, -1) << endl;
  return 0;
}