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

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

struct page {
  int sum;
  int x1[110], y1[110], x2[110], y2[110];
  int num[110];
  string name[110];
  string my_name;
} a[110];

char s0[110];
string s;

map<string, int> g;

int n, m;
int x0, y0;
int c[1100000];
int top, now;

int main() {
  // freopen("in.txt","r",stdin);
  while (cin >> n) {
    if (n == 0)
      return 0;
    cin >> x0 >> y0;
    g.clear();
    for (int k = 1; k <= n; k++) {
      scanf("%s", s0);
      s = s0;
      g[s0] = k;
      a[k].my_name = s0;
      scanf("%d", &a[k].sum);
      for (int i = 1; i <= a[k].sum; i++) {
        scanf("%d%d%d%d %s", &a[k].x1[i], &a[k].y1[i], &a[k].x2[i], &a[k].y2[i],
              s0);
        a[k].name[i] = s0;
      }
    }
    for (int k = 1; k <= n; k++)
      for (int i = 1; i <= a[k].sum; i++)
        a[k].num[i] = g[a[k].name[i]];
    top = 1;
    now = 1;
    c[1] = 1;
    cin >> m;
    for (int tt = 1; tt <= m; tt++) {
      scanf("%s", s0);
      if (strcmp(s0, "show") == 0) {
        for (int i = 0; i < a[c[now]].my_name.length(); i++)
          printf("%c", a[c[now]].my_name[i]);
        puts("");
      }
      if (strcmp(s0, "click") == 0) {
        int xx, yy;
        scanf("%d%d", &xx, &yy);
        int id = c[now];
        int nx_num = 0;
        for (int k = 1; k <= a[id].sum; k++) {
          if (xx >= a[id].x1[k] && xx <= a[id].x2[k] && yy >= a[id].y1[k] &&
              yy <= a[id].y2[k])
            nx_num = k;
          if (nx_num)
            break;
        }
        if (nx_num != 0) {
          c[++now] = a[id].num[nx_num];
          top = now;
        }
      }
      if (strcmp(s0, "back") == 0) {
        if (now > 1)
          now--;
      }
      if (strcmp(s0, "forward") == 0) {
        if (now < top)
          now++;
      }
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <fstream>
#include <iostream>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
struct myLink {
  string paname, liname;
  int x1, x2, y1, y2;
};

int main() {
  // ifstream cin("input.txt");
  // ofstream cout("output.txt");
  for (int n; cin >> n && n;) {
    myLink li[10001];
    int w, h;
    cin >> w >> h;
    int pos = 0, num;
    string name, s;
    for (int i = 0; i < n; i++, pos += num) {
      cin >> name >> num;
      for (int j = pos; j - pos < num; j++) {
        cin >> li[j].x1 >> li[j].y1 >> li[j].x2 >> li[j].y2 >> li[j].liname;
        li[j].paname = name;
      }
    }
    string buf[1001] = {li[0].paname};
    int point = 0, m;
    cin >> m;
    while (m--) {
      cin >> s;
      if (s[0] == 'c') {
        int x, y;
        cin >> x >> y;
        for (int j = 0; j < pos; j++) {
          if (li[j].paname == buf[point] && li[j].x1 <= x && x <= li[j].x2 &&
              li[j].y1 <= y && y <= li[j].y2) {
            point++;
            buf[point] = li[j].liname;
            for (int k = point + 1; k < 1000; k++)
              buf[k] = "";
          }
        }
      } else if (s[0] == 's')
        cout << buf[point] << "\\n";

      else if (s[0] == 'b' && point >= 1)
        point--;

      else if (s[0] == 'f' && buf[point + 1] != "")
        point++;
    }
  }
}