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

#include <iostream>
#include <string>
using namespace std;
int main() {
  string str[1000];
  string s;
  int size = 0;
  while (getline(cin, s)) {
    if (s == "END_OF_TEXT")
      break;
    str[size++] = s;
  }
  char ch;
  string buf = "";
  int line = 0, pos = 0;
  while (cin >> ch) {
    if (ch == '-')
      break;
    if (ch == 'a')
      pos = 0;
    if (ch == 'e')
      pos = str[line].size();
    if (ch == 'p') {
      pos = 0;
      if (line != 0)
        line--;
    }
    if (ch == 'n') {
      pos = 0;
      if (line + 1 != size)
        line++;
    }
    if (ch == 'f') {
      if (pos != str[line].size())
        pos++;
      else if (line + 1 != size) {
        pos = 0;
        line++;
      }
    }
    if (ch == 'b') {
      if (pos != 0)
        pos--;
      else if (line != 0) {
        line--;
        pos = str[line].size();
      }
    }
    if (ch == 'd') {
      if (pos != str[line].size()) {
        str[line] = str[line].substr(0, pos) + str[line].substr(pos + 1);
      } else if (line + 1 != size) {
        str[line] = str[line] + str[line + 1];
        for (int i = line + 1; i + 1 < size; i++) {
          str[i] = str[i + 1];
        }
        str[size - 1] = "";
        size--;
      }
    }
    if (ch == 'k') {
      if (pos == str[line].size() && line + 1 != size) {
        str[line] = str[line] + str[line + 1];
        for (int i = line + 1; i + 1 < size; i++) {
          str[i] = str[i + 1];
        }
        str[size - 1] = "";
        size--;
        buf = "IS_BREAK_LINE";
      } else if (pos < str[line].size()) {
        buf = str[line].substr(pos);
        str[line] = str[line].substr(0, pos);
      }
    }
    if (ch == 'y') {
      if (buf == "IS_BREAK_LINE") {
        for (int i = size - 1; i >= line + 1; i--) {
          str[i + 1] = str[i];
        }
        size++;
        str[line + 1] = str[line].substr(pos);
        str[line] = str[line].substr(0, pos);
        line++;
        pos = 0;
      } else if (buf != "") {
        str[line] = str[line].substr(0, pos) + buf + str[line].substr(pos);
        pos += buf.size();
      }
    } /* cout << line << " " << pos << endl; for(int i=0;i<size;i++){ cout <<
         str[i] << endl; } puts(""); */
  }
  for (int i = 0; i < size; i++) {
    cout << str[i] << endl;
  }
}

Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::substr: __pos (which is 18446744073709551615) > this->size() (which is 0)


Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;

int main() {
  string s[500];
  int line = 0;
  for (string in; cin >> in, in != "END_OF_TEXT"; ++line) {
    s[line] = in;
  }

  int pX = 0;
  int pY = 0;

  string buf;
  for (string in; cin >> in, in != "-";) {
    if (in == "a") {
      pX = 0;
    } else if (in == "e") {
      pX = s[pY].length();
    } else if (in == "p") {
      pX = 0;
      if (0 < pY) {
        --pY;
      }
    } else if (in == "n") {
      pX = 0;
      if (pY < line - 1) {
        ++pY;
      }
    } else if (in == "f") {
      if (pX != s[pY].length()) {
        ++pX;
      } else {
        if (pY < line - 1) {
          pX = 0;
          ++pY;
        }
      }
    } else if (in == "b") {
      if (0 < pX) {
        --pX;
      } else {
        if (0 < pY) {
          --pY;
          pX = s[pY].length();
        }
      }
    } else if (in == "d") {
      if (pX != s[pY].length()) {
        s[pY] = s[pY].substr(0, pX) + s[pY].substr(pX + 1);
      } else if (pY < line - 1) {
        s[pY] = s[pY] + s[pY + 1];
        for (int i = pY + 1; i < line - 1; ++i) {
          s[i] = s[i + 1];
        }
        --line;
      }
    } else if (in == "k") {
      if (pX != s[pY].length()) {
        buf = s[pY].substr(pX);
        s[pY] = s[pY].substr(0, pX);
        pX = s[pY].length() - 1;
      } else if (pY < line - 1) {
        s[pY] = s[pY] + s[pY + 1];
        for (int i = pY + 1; i < line - 1; ++i) {
          s[i] = s[i + 1];
        }
        --line;
        buf = "\\n";
      }
    } else if (in == "y") {
      if (!buf.empty()) {
        if (buf == "\\n") {
          for (int i = line - 1; pY < i; --i) {
            s[i] = s[i - 1];
          }
          s[pY + 1] = s[pY].substr(pX);
          s[pY] = s[pY].substr(0, pX);
          ++line;
        } else {
          s[pY] = s[pY].substr(0, pX) + buf + s[pY].substr(pX);
        }
      }
    }
  }

  for (int i = 0; i < line; ++i)
    cout << s[i] << endl;
}