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

#include <algorithm>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

struct Obj {
  bool isStr;
  string str;
  map<string, Obj *> pro;
};

vector<pair<int, string>> v;
int pos;

void make(Obj *obj) {
  for (int i = 0, ppos; pos < v.size(); ++i) {
    if (i && v[ppos].first != v[pos].first)
      break;
    Obj *nobj = new Obj();
    string &target = v[pos].second;
    ppos = pos++;

    int j;
    for (j = 0; target[j] != ':'; ++j)
      ;
    string key = target.substr(0, j);

    if (j + 1 == target.size()) {
      nobj->isStr = false;
      make(nobj);
    } else {
      nobj->isStr = true;
      nobj->str = target.substr(j + 2);
    }
    obj->pro[key] = nobj;
  }
}

int main() {
  string p;
  getline(cin, p);
  string line;
  while (getline(cin, line)) {
    int sp = 0;
    while (line[0] == ' ') {
      ++sp;
      line = line.substr(1);
    }
    v.push_back(make_pair(sp, line));
  }

  Obj *root = new Obj();
  root->isStr = false;
  pos = 0;
  make(root);

  vector<string> ps;
  {
    replace(p.begin(), p.end(), '.', ' ');
    stringstream ss(p);
    string s;
    while (ss >> s)
      ps.push_back(s);
  }

  Obj *obj = root;
  for (int i = 0; i < ps.size(); ++i) {
    if (obj->pro.count(ps[i])) {
      obj = obj->pro[ps[i]];
    } else {
      obj = 0;
      break;
    }
  }
  if (obj) {
    if (obj->isStr)
      cout << "string \\"" << obj->str << "\\"" << endl;
    else
      cout << "object" << endl;
  } else {
    cout << "no such property" << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

struct Obj {
  bool isStr;
  string str;
  map<string, Obj *> pro;
};

string p;
vector<pair<int, string>> v;
int pos;

void make(Obj *obj) {
  for (int i = 0; pos < v.size(); ++i) {
    if (i && v[pos - 1].first != v[pos].first)
      break;
    Obj *nobj = new Obj();
    string &target = v[pos].second;
    ++pos;
    int j;
    for (j = 0; j < target.size(); ++j) {
      if (target[j] == ':')
        break;
    }
    string key = target.substr(0, j);

    if (j + 1 == target.size()) {
      nobj->isStr = false;
      make(nobj);
    } else {
      nobj->isStr = true;
      nobj->str = target.substr(j + 2);
    }
    obj->pro[key] = nobj;
  }
}

int main() {
  getline(cin, p);
  while (!cin.eof()) {
    string line;
    getline(cin, line);
    int sp = 0;
    while (line[0] == ' ') {
      ++sp;
      line = line.substr(1);
    }
    v.push_back(make_pair(sp, line));
  }

  Obj *root = new Obj();
  make(root);

  vector<string> ps;
  {
    replace(p.begin(), p.end(), '.', ' ');
    stringstream ss(p);
    while (!ss.eof()) {
      string s;
      ss >> s;
      ps.push_back(s);
    }
  }

  Obj *obj = root;
  for (int i = 0; i < ps.size(); ++i) {
    if (obj->pro.count(ps[i])) {
      obj = obj->pro[ps[i]];
    } else {
      obj = 0;
      break;
    }
  }
  if (obj) {
    if (obj->isStr)
      cout << "string \\"" << obj->str << "\\"" << endl;
    else
      cout << "object" << endl;
  } else {
    cout << "no such property" << endl;
  }

  return 0;
}