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

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

struct POINT {
  int x1, y1;
  int x2, y2;
};

multimap<string, POINT> MakeButton(int);
void Click(map<string, multimap<string, POINT>>, vector<string> &, int &, int,
           int);

int main() {
  int i, n, m, w, h, now;
  string str;
  vector<string> logdata;
  map<string, multimap<string, POINT>> webpage;

  while (1) {
    cin >> n;
    if (n == 0)
      break;
    cin >> w >> h;

    for (i = 0; i < n; ++i) {
      cin >> str >> m;
      if (i == 0)
        logdata.push_back(str);
      webpage.insert(make_pair(str, MakeButton(m)));
    }

    cin >> m;
    for (i = 0, now = 0; i < m; ++i) {
      cin >> str;
      if (str == "click")
        Click(webpage, logdata, now, w, h);
      else if (str == "back" && now > 0)
        --now;
      else if (str == "forward" && now < logdata.size() - 1)
        ++now;
      else if (str == "show")
        cout << logdata[now] << endl;
    }

    logdata.clear();
    webpage.clear();
  }
  return 0;
}

multimap<string, POINT> MakeButton(int n) {
  int i, x1, y1, x2, y2;
  string str;
  POINT p;
  multimap<string, POINT> button;

  for (i = 0; i < n; ++i) {
    cin >> p.x1 >> p.y1 >> p.x2 >> p.y2 >> str;
    button.insert(make_pair(str, p));
  }
  return button;
}

void Click(map<string, multimap<string, POINT>> webpage,
           vector<string> &logdata, int &now, int w, int h) {
  int x, y;
  cin >> x >> y;
  if (x < 0 || w < x || y < 0 || h < y)
    return;

  POINT p;
  multimap<string, POINT> button;
  multimap<string, POINT>::iterator i;
  map<string, multimap<string, POINT>>::iterator j;

  j = webpage.find(logdata[now]);
  if (j == webpage.end())
    return;
  button = j->second;

  for (i = button.begin(); i != button.end(); ++i) {
    p = i->second;
    if (p.x1 <= x && x <= p.x2 && p.y1 <= y && y <= p.y2) {
      ++now;
      logdata.erase(logdata.begin() + now, logdata.end());
      logdata.push_back(i->first);
      return;
    }
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

struct POINT {
  int x1, y1;
  int x2, y2;
};

map<string, POINT> MakeButton(int);
void Click(map<string, map<string, POINT>>, vector<string> &, int &);

int main() {
  int i, j, n, m, w, h, now;
  string str;
  vector<string> logdata;
  map<string, map<string, POINT>> webpage;

  while (1) {
    cin >> n;
    if (n == 0)
      break;
    cin >> w >> h;

    for (i = 0; i < n; ++i) {
      cin >> str >> m;
      if (i == 0)
        logdata.push_back(str);
      webpage.insert(make_pair(str, MakeButton(m)));
    }

    cin >> m;
    for (i = 0, now = 0; i < m; ++i) {
      cin >> str;
      if (str == "click")
        Click(webpage, logdata, now);
      else if (str == "back")
        --now;
      else if (str == "forward")
        ++now;
      else if (str == "show")
        cout << logdata[now] << endl;
    }

    logdata.clear();
    webpage.clear();
  }
  return 0;
}

map<string, POINT> MakeButton(int n) {
  int i, x1, y1, x2, y2;
  string str;
  POINT p;
  map<string, POINT> button;

  for (i = 0; i < n; ++i) {
    cin >> p.x1 >> p.y1 >> p.x2 >> p.y2 >> str;
    button.insert(make_pair(str, p));
  }
  return button;
}

void Click(map<string, map<string, POINT>> webpage, vector<string> &logdata,
           int &now) {
  int x, y;
  POINT p;
  map<string, POINT> button;
  map<string, POINT>::iterator i;
  map<string, map<string, POINT>>::iterator j;

  j = webpage.find(logdata[now]);
  button = j->second;
  cin >> x >> y;

  for (i = button.begin(); i != button.end(); ++i) {
    p = i->second;
    if (p.x1 <= x && x <= p.x2 && p.y1 <= y && y <= p.y2) {
      ++now;
      logdata.erase(logdata.begin() + now, logdata.end());
      logdata.push_back(i->first);
      return;
    }
  }
}