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

#include <cctype>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class EmacsLikeEditor {
public:
  vector<string> text;
  int cur;
  int line;
  string buf;
  EmacsLikeEditor(const vector<string> &vs) {
    cur = 0;
    line = 0;
    buf = "";
    for (int i = 0; i < vs.size(); ++i) {
      text.push_back("");
      for (int j = 0; j < vs[i].length(); ++j) {
        // if( !iscntrl( vs[i][j] ) ){
        text[i] += vs[i][j];
        //}
      }
      text[i] += "$";
    }
  }
  void Command(char c) {
    switch (c) {
    case 'a':
      MoveLineFront();
      break;
    case 'e':
      MoveLineEnd();
      break;
    case 'p':
      MoveAbove();
      break;
    case 'n':
      MoveBelow();
      break;
    case 'f':
      MoveFront();
      break;
    case 'b':
      MoveBack();
      break;
    case 'd':
      Delete();
      break;
    case 'k':
      CutLine();
      break;
    case 'y':
      Paste();
      break;
    default:
      break;
    }
  }
  bool ExistAboveLine() { return line > 0; }
  bool ExistBelowLine() { return line < text.size() - 1; }
  bool FrontOfLine() { return cur == 0; }
  bool EndOfLine() { return cur == text[line].size() - 1; }

  void MoveLineFront() { cur = 0; }
  void MoveLineEnd() { cur = text[line].size() - 1; }

  void MoveAbove() {
    if (ExistAboveLine()) {
      --line;
    }
    MoveLineFront();
  }
  void MoveBelow() {
    if (ExistBelowLine()) {
      ++line;
    }
    MoveLineFront();
  }

  void MoveFront() {
    if (!EndOfLine()) {
      ++cur;
    } else {
      if (ExistBelowLine()) {
        ++line;
        cur = 0;
      }
    }
  }
  void MoveBack() {
    if (!FrontOfLine()) {
      --cur;
    } else {
      if (ExistAboveLine()) {
        --line;
        cur = text[line].size() - 1;
      }
    }
  }

  void Delete() {
    if (!EndOfLine()) {
      text[line].erase(text[line].begin() + cur);
    } else if (ExistBelowLine()) {
      text[line].erase(text[line].end() - 1);
      for (int i = 0; i < text[line + 1].length(); ++i) {
        text[line] += text[line + 1][i];
      }
      text.erase(text.begin() + line + 1);
    }
  }
  void CutLine() {
    if (EndOfLine()) {
      if (ExistBelowLine()) {
        buf = "$";
        Delete();
      }
    } else {
      buf = string(text[line].begin() + cur, text[line].end() - 1);
      text[line].erase(text[line].begin() + cur, text[line].end() - 1);
      cur = text[line].size() - 1;
    }
  }
  void Paste() {
    if (buf == "")
      return;
    else {
      if (buf == "$") {
        text.insert(text.begin() + line + 1,
                    string(text[line].begin() + cur, text[line].end()));
        text[line].erase(text[line].begin() + cur, text[line].end() - 1);
        ++line;
        cur = 0;
      } else {
        text[line].insert(text[line].begin() + cur, buf.begin(), buf.end());
        cur += buf.length();
      }
    }
  }
};
int main() {

  vector<string> vs;
  while (true) {
    string s;
    getline(cin, s);
    // while(iscntrl(s[s.length()-1]))s.erase(s.end()-1);
    if (s == "END_OF_TEXT")
      break;
    vs.push_back(s);
  }
  EmacsLikeEditor ele(vs);
  while (true) {
    char command;
    cin >> command;
    if (command == '-')
      break;
    ele.Command(command);
  }

  for (int i = 0; i < ele.text.size(); ++i) {
    ele.text[i].erase(ele.text[i].end() - 1);
    cout << ele.text[i] << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <cctype>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class EmacsLikeEditor {
public:
  vector<string> text;
  int cur;
  int line;
  string buf;
  EmacsLikeEditor(const vector<string> &vs) {
    cur = 0;
    line = 0;
    for (int i = 0; i < vs.size(); ++i) {
      text.push_back("");
      for (int j = 0; j < vs[i].length(); ++j) {
        if (!iscntrl(vs[i][j])) {
          text[i] += vs[i][j];
        }
      }
      text[i] += "$";
    }
  }
  void Command(char c) {
    switch (c) {
    case 'a':
      MoveLineFront();
      break;
    case 'e':
      MoveLineEnd();
      break;
    case 'p':
      MoveAbove();
      break;
    case 'n':
      MoveBelow();
      break;
    case 'f':
      MoveFront();
      break;
    case 'b':
      MoveBack();
      break;
    case 'd':
      Delete();
      break;
    case 'k':
      CutLine();
      break;
    case 'y':
      Paste();
      break;
    default:
      break;
    }
  }
  bool ExistAboveLine() { return line > 0; }
  bool ExistBelowLine() { return line < text.size() - 1; }
  bool FrontOfLine() { return cur == 0; }
  bool EndOfLine() { return cur == text[line].size() - 1; }

  void MoveLineFront() { cur = 0; }
  void MoveLineEnd() { cur = text[line].size() - 1; }

  void MoveAbove() {
    if (ExistAboveLine()) {
      --line;
    }
    MoveLineFront();
  }
  void MoveBelow() {
    if (ExistBelowLine()) {
      ++line;
    }
    MoveLineFront();
  }

  void MoveFront() {
    if (!EndOfLine()) {
      ++cur;
    } else {
      if (ExistBelowLine()) {
        ++line;
        cur = 0;
      }
    }
  }
  void MoveBack() {
    if (!FrontOfLine()) {
      --cur;
    } else {
      if (ExistAboveLine()) {
        --line;
        cur = text[line].size() - 1;
      }
    }
  }

  void Delete() {
    if (!EndOfLine()) {
      text[line].erase(text[line].begin() + cur);
    } else if (ExistBelowLine()) {
      text[line].erase(text[line].end() - 1);
      for (int i = 0; i < text[line + 1].length(); ++i) {
        text[line] += text[line + 1][i];
      }
    }
  }
  void CutLine() {
    if (EndOfLine()) {
      if (ExistBelowLine()) {
        buf = "$";
        Delete();
      }
    } else {
      buf = "";
      for (int i = cur; i < text[line].size() - 1; ++i) {
        buf += text[line][i];
      }
      text[line].erase(text[line].begin() + cur, text[line].end() - 1);
      cur = text[line].size() - 1;
    }
  }
  void Paste() {
    if (buf == "")
      return;
    else {
      if (buf == "$") {
        text.insert(text.begin() + line + 1,
                    string(text[line].begin() + cur, text[line].end()));
        text[line].erase(text[line].begin() + cur, text[line].end());
        text[line] += '$';
        ++line;
        cur = 0;
      } else {
        text[line].insert(text[line].begin() + cur, buf.begin(), buf.end());
        cur += buf.length();
      }
    }
  }
};
int main() {

  vector<string> vs;
  while (true) {
    string s;
    getline(cin, s);
    while (iscntrl(s[s.length() - 1]))
      s.erase(s.end() - 1);
    if (s == "END_OF_TEXT")
      break;
    vs.push_back(s);
  }
  EmacsLikeEditor ele(vs);
  while (true) {
    char command;
    cin >> command;
    if (command == '-')
      break;
    ele.Command(command);
  }

  for (int i = 0; i < ele.text.size(); ++i) {
    ele.text[i].erase(ele.text[i].end() - 1);
    cout << ele.text[i] << endl;
  }

  return 0;
}