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

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  int p, q;
  string str, buf;
  vector<string> vec;

  while (!cin.eof()) {
    p = q = 0;
    buf = "";

    while (getline(cin, str) && str != "END_OF_TEXT")
      vec.push_back(str);
    while (getline(cin, str) && str != "-") {
      if (str == "a")
        q = 0;

      if (str == "e")
        q = vec[p].size();

      if (str == "p") {
        if (p > 0)
          --p;
        q = 0;
      }

      if (str == "n") {
        if (p < vec.size() - 1)
          ++p;
        q = 0;
      }

      if (str == "f") {
        if (q < vec[p].size())
          ++q;
        else if (q == vec[p].size() && p < vec.size() - 1) {
          ++p;
          q = 0;
        }
      }

      if (str == "b") {
        if (q > 0)
          --q;
        else if (q == 0 && p > 0) {
          --p;
          q = vec[p].size();
        }
      }

      if (str == "d") {
        if (q == vec[p].size()) {
          if (vec.size() - 1 > p) {
            vec[p] += vec[p + 1];
            vec.erase(vec.begin() + (p + 1));
          }
        } else if (q < vec[p].size()) {
          vec[p].erase(vec[p].begin() + q);
        }
      }

      if (str == "k") {
        if (q == vec[p].size()) {
          if (vec.size() - 1 > p) {
            vec[p] += vec[p + 1];
            vec.erase(vec.begin() + (p + 1));
            buf = "\\n";
          }
        } else if (q < vec[p].size()) {
          buf = vec[p].substr(q);
          vec[p] = vec[p].substr(0, q);
        }
      }

      if (str == "y" && buf.size()) {
        if (buf == "\\n") {
          string s1 = vec[p].substr(0, q), s2 = vec[p].substr(q);
          vec[p++] = s1;
          vec.insert(vec.begin() + (p), s2);
          q = 0;
        } else {
          vec[p] = vec[p].substr(0, q) + buf + vec[p].substr(q);
          q += buf.size();
        }
      }
    }

    for (int i = 0; i < vec.size(); ++i)
      cout << vec[i] << endl;

    buf.clear();
    vec.clear();
  }

  return 0;
}

Error: -11

Fixed submission:

#include <cctype>
#include <iostream>
#include <string>

using namespace std;

string kaigyou = "wararaki_wararaki_kaigyo";

int main() {
  int p, q, w[101], h;
  string str, buf;
  char ch[101][1001];

  while (!cin.eof()) {
    getline(cin, str);
    fill(w, w + 101, 0);
    h = 0;
    while (str != "END_OF_TEXT") {
      for (int i = 0; i < str.size(); i++) {
        ch[h][i] = str[i];
      }
      w[h++] = str.size();
      getline(cin, str);
    }
    p = q = 0;
    buf = "";
    while (1) {
      getline(cin, str);
      if (str == "-") {
        break;
      } else if (str == "a") {
        q = 0;
      } else if (str == "e") {
        q = w[p];
      } else if (str == "p") {
        if (p > 0) {
          p--;
        }
        q = 0;
      } else if (str == "n") {
        if (p < h - 1) {
          p++;
        }
        q = 0;
      } else if (str == "f") {
        if (q < w[p]) {
          q++;
        } else if (p < h - 1) {
          p++;
          q = 0;
        }
      } else if (str == "b") {
        if (q > 0) {
          q--;
        } else if (p > 0) {
          p--;
          q = w[p];
        }
      } else if (str == "d") {
        if (q == w[p]) {
          if (p < h - 1) {
            for (int i = 0; i < w[p + 1]; i++) {
              ch[p][w[p] + i] = ch[p + 1][i];
            }
            w[p] += w[p + 1];
            for (int i = p + 2; i < h; i++) {
              for (int j = 0; j < w[i]; j++) {
                ch[i - 1][j] = ch[i][j];
              }
              w[i - 1] = w[i];
            }

            w[--h] = 0;
          }
        } else if (isalpha(ch[p][q])) {
          for (int i = 0, t = 0; i < w[p]; i++) {
            if (i != q) {
              ch[p][t++] = ch[p][i];
            }
          }
          w[p]--;
        }
      } else if (str == "k") {
        if (q == w[p] && p < h - 1) {
          buf = kaigyou;
          for (int i = 0; i < w[p + 1]; i++) {
            ch[p][w[p] + i] = ch[p + 1][i];
          }
          w[p] += w[p + 1];
          for (int i = p + 2; i < h; i++) {
            for (int j = 0; j < w[i]; j++) {
              ch[i - 1][j] = ch[i][j];
            }
            w[i - 1] = w[i];
          }
          w[--h] = 0;
        } else if (q < w[p]) {
          buf = "";
          for (int i = q; i < w[p]; i++) {
            buf += ch[p][i];
          }
          w[p] = q;
        }
      } else if (str == "y") {
        if (buf.size() > 0) {
          if (buf == kaigyou) {
            string tmp = "";
            for (int i = q; i < w[p]; i++) {
              tmp += ch[p][i];
            }
            w[p] = q;
            for (int i = h; i > p; i--) {
              for (int j = 0; j < w[i - 1]; j++) {
                ch[i][j] = ch[i - 1][j];
              }
              w[i] = w[i - 1];
            }
            h++;
            w[p + 1] = tmp.size();
            for (int i = 0; i < w[p + 1]; i++) {
              ch[p + 1][i] = tmp[i];
            }
            q = 0;
            p++;
          } else {
            string tmp = "";
            for (int i = 0; i < q; i++) {
              tmp += ch[p][i];
            }
            tmp += buf;
            for (int i = q; i < w[p]; i++) {
              tmp += ch[p][i];
            }
            for (int i = 0; i < tmp.size(); i++) {
              ch[p][i] = tmp[i];
            }
            q = w[p] = tmp.size();
          }
        }
      }
    }
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w[i]; j++) {
        cout << ch[i][j];
      }
      cout << endl;
    }
  }

  return 0;
}