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

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

using namespace std;

#define sz size()
#define er erase
#define ss substr

class Emacs {
public:
  Emacs() {
    w = h = 0;
    buf.clear();
  }
  void appendLine(string line) { txt.push_back(line); }
  void execCommand(char code) {
    switch (code) {
    case 'a':
      A();
      break;
    case 'e':
      E();
      break;
    case 'p':
      P();
      break;
    case 'n':
      N();
      break;
    case 'f':
      F();
      break;
    case 'b':
      B();
      break;
    case 'd':
      D();
      break;
    case 'k':
      K();
      break;
    case 'y':
      Y();
      break;
    }
  }
  void showText() {
    for (auto line : txt)
      cout << line << "\\n";
  }

private:
  void A() { w = 0; }
  void E() { w = txt[h].sz; }
  void P() {
    h = max(h - 1, 0);
    A();
  }
  void N() {
    h = min(h + 1, (int)txt.sz - 1);
    A();
  }
  void F() {
    if (w < txt[h].sz)
      w++;
    else if (h < txt.sz - 1)
      A(), h++;
  }
  void B() {
    if (w > 0)
      w--;
    else if (h > 0)
      h--, E();
  }
  void D() {
    if (w < txt[h].sz)
      txt[h].er(w, 1);
    else if (h < txt.sz - 1)
      txt[h] += txt[h + 1], txt.er(txt.begin() + h + 1);
  }
  void K() {
    if (w < txt[h].sz)
      buf = txt[h].ss(w), txt[h] = txt[h].ss(0, w);
    else if (h < txt.sz - 1)
      D(), buf = "\\n";
  }
  void Y() {
    if (!buf.empty()) {
      if (buf == "\\n")
        txt.insert(txt.begin() + h + 1, txt[h].ss(w)), txt[h].er(w), N();
      else
        txt[h].insert(w, buf), w += buf.sz;
    }
  }
  vector<string> txt;
  string buf;
  int h, w;
};

int main() {
  Emacs emacs;

  string in;
  while (getline(cin, in), in != "END_OF_TEXT")
    emacs.appendLine(in);

  char cmd;
  while (cin >> cmd, cmd != '-')
    emacs.execCommand(cmd);

  emacs.showText();

  return (0);
}

Error: WA

Fixed submission:

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

using namespace std;

#define sz size()
#define er erase
#define ss substr

class Emacs {
public:
  Emacs() {
    w = h = 0;
    buf.clear();
  }
  void appendLine(string line) { txt.push_back(line); }
  void execCommand(char code) {
    switch (code) {
    case 'a':
      A();
      break;
    case 'e':
      E();
      break;
    case 'p':
      P();
      break;
    case 'n':
      N();
      break;
    case 'f':
      F();
      break;
    case 'b':
      B();
      break;
    case 'd':
      D();
      break;
    case 'k':
      K();
      break;
    case 'y':
      Y();
      break;
    }
  }
  void showText() {
    for (auto line : txt)
      cerr << line << "\\n";
  }

private:
  void A() { w = 0; }
  void E() { w = txt[h].sz; }
  void P() {
    h = max(h - 1, 0);
    A();
  }
  void N() {
    h = min(h + 1, (int)txt.sz - 1);
    A();
  }
  void F() {
    if (w < txt[h].sz)
      w++;
    else if (h < txt.sz - 1)
      A(), h++;
  }
  void B() {
    if (w > 0)
      w--;
    else if (h > 0)
      h--, E();
  }
  void D() {
    if (w < txt[h].sz)
      txt[h].er(w, 1);
    else if (h < txt.sz - 1)
      txt[h] += txt[h + 1], txt.er(txt.begin() + h + 1);
  }
  void K() {
    if (w < txt[h].sz)
      buf = txt[h].ss(w), txt[h] = txt[h].ss(0, w);
    else if (h < txt.sz - 1)
      D(), buf = "\\n";
  }
  void Y() {
    if (!buf.empty()) {
      if (buf == "\\n")
        txt.insert(txt.begin() + h + 1, txt[h].ss(w));
      else
        txt[h].insert(w, buf), w += buf.sz;
    }
  }
  vector<string> txt;
  string buf;
  int h, w;
};

int main() {
  Emacs emacs;

  string in;
  while (getline(cin, in), in != "END_OF_TEXT")
    emacs.appendLine(in);

  char cmd;
  while (cin >> cmd, cmd != '-')
    emacs.execCommand(cmd);

  emacs.showText();

  return (0);
}