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

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

using namespace std;

int main() {
  vector<string> text(200);
  int line = 0;
  for (; line < 11; line++) {
    getline(cin, text[line], '\\n');
    if (text[line] == "END_OF_TEXT")
      break;
  }
  string buf = "";
  int cur[2];
  cur[0] = 0; // 0s
  cur[1] = 0; // 0¶Ú

  while (true) {
    char com;
    cin >> com;
    if (com == '-')
      break;
    switch (com) {
    case 'a':
      cur[1] = 0;
      break;
    case 'e':
      cur[1] = (int)text[cur[0]].size();
      break;
    case 'p':
      if (cur[0] > 0)
        cur[0]--;
      cur[1] = 0;
      break;
    case 'n':
      if (cur[0] < line - 1)
        cur[0]++;
      cur[1] = 0;
      break;
    case 'f':
      if (cur[1] < (int)text[cur[0]].size()) {
        cur[1]++;
      } else {
        if (cur[0] < line - 1) {
          cur[0]++;
          cur[1] = 0;
        }
      }
      break;
    case 'b':
      if (cur[1] > 0)
        cur[1]--;
      else {
        if (cur[0] > 0) {
          cur[0]--;
          cur[1] = (int)text[cur[0]].size();
        }
      }
      break;
    case 'd':
      if (cur[1] < (int)text[cur[0]].size()) {
        text[cur[0]] =
            text[cur[0]].substr(0, cur[1]) + text[cur[0]].substr(cur[1] + 1);
      } else {
        if (cur[0] < line - 1) {
          text[cur[0]] = text[cur[0]] + text[cur[0] + 1];
          for (int i = cur[0] + 1; i < line + 1; i++)
            text[i] = text[i + 1];
          line--;
        }
      }
      break;
    case 'k':
      if (cur[1] == (int)text[cur[0]].size()) {
        if (cur[0] < line - 1) {
          text[cur[0]] = text[cur[0]] + text[cur[0] + 1];
          for (int i = cur[0] + 1; i < line + 1; i++)
            text[i] = text[i + 1];
          line--;
          buf = "\\n";
        }
      } else {
        buf = text[cur[0]].substr(cur[1]);
        text[cur[0]] = text[cur[0]].substr(0, cur[1]);
        cur[1] = (int)text[cur[0]].size();
      }
      break;
    case 'y':
      if (buf.empty())
        break;
      else {
        if (buf == "\\n") {
          for (int i = line + 1; i >= cur[0] + 1; i--)
            text[i + 1] = text[i];
          line++;
          text[cur[0] + 1] = text[cur[0]].substr(cur[1]);
          text[cur[0]] = text[cur[0]].substr(0, cur[1]);
          cur[0]++;
          cur[1] = 0;
        } else {
          text[cur[0]] = text[cur[0]].substr(0, cur[1]) + buf +
                         text[cur[0]].substr(cur[1]);
          cur[1] += buf.size();
        }
      }
    default:
      break;
    }
    /*         for(int i=0;i<line;i++)
        cout << text[i] << endl;
        cout << "(" << cur[0] << "," << cur[1] << ")" << endl;
        cout << "#" << endl;*/
  }
  for (int i = 0; i < line; i++)
    cout << text[i] << endl;
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  vector<string> text(200);
  int line = 0;
  for (; line < 11; line++) {
    getline(cin, text[line], '\\n');
    if (text[line] == "END_OF_TEXT")
      break;
  }
  string buf;
  int cur[2];
  cur[0] = 0; // 0s
  cur[1] = 0; // 0¶Ú

  while (true) {
    char com;
    cin >> com;
    if (com == '-')
      break;
    switch (com) {
    case 'a':
      cur[1] = 0;
      break;
    case 'e':
      cur[1] = (int)text[cur[0]].size();
      break;
    case 'p':
      if (cur[0] > 0)
        cur[0]--;
      cur[1] = 0;
      break;
    case 'n':
      if (cur[0] < line - 1)
        cur[0]++;
      cur[1] = 0;
      break;
    case 'f':
      if (cur[1] < (int)text[cur[0]].size())
        cur[1]++;
      else {
        if (cur[0] < line - 1) {
          cur[0]++;
          cur[1] = 0;
        }
      }
      break;
    case 'b':
      if (cur[1] > 0)
        cur[1]--;
      else {
        if (cur[0] > 0) {
          cur[0]--;
          cur[1] = (int)text[cur[0]].size();
        }
      }
      break;
    case 'd':
      if (cur[1] < (int)text[cur[0]].size()) {
        text[cur[0]] =
            text[cur[0]].substr(0, cur[1]) + text[cur[0]].substr(cur[1] + 1);
      } else {
        if (cur[0] < line - 1) {
          text[cur[0]] = text[cur[0]] + text[cur[0] + 1];
          for (int i = cur[0] + 1; i < line + 1; i++)
            text[i] = text[i + 1];
          line--;
        }
      }
      break;
    case 'k':
      if (cur[1] == (int)text[cur[0]].size()) {
        if (cur[0] < line - 1) {
          text[cur[0]] = text[cur[0]] + text[cur[0] + 1];
          for (int i = cur[0] + 1; i < line + 1; i++)
            text[i] = text[i + 1];
          line--;
          buf = "\\n";
        }
      } else {
        buf = text[cur[0]].substr(cur[1]);
        text[cur[0]] = text[cur[0]].substr(0, cur[1]);
        cur[1] = (int)text[cur[0]].size();
      }
      break;
    case 'y':
      if (buf.empty())
        break;
      else {
        if (buf == "\\n") {
          for (int i = cur[0] + 1; i < line + 1; i++)
            text[i + 1] = text[i];
          line++;
          text[cur[0]] = text[cur[0]].substr(cur[1]);
          text[cur[0] + 1] = text[cur[0]].substr(0, cur[1]);
        } else {
          text[cur[0]] = text[cur[0]].substr(0, cur[1]) + buf +
                         text[cur[0]].substr(cur[1]);
        }
      }
    default:
      break;
    }
    /*  for(int i=0;i<line;i++)
        cout << text[i] << endl;
        cout << "(" << cur[0] << "," << cur[1] << ")" << endl;
        cout << "#" << endl;*/
  }
  for (int i = 0; i < line; i++)
    cout << text[i] << endl;
  return 0;
}