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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int main() {
  int x = 0;
  list<string> s;
  string t, b;
  while (getline(cin, t), t != "END_OF_TEXT")
    s.push_back(t);
  list<string>::iterator y = s.begin();
  char c;
  while (cin >> c, c != '-') {
    switch (c) {
    case 'a':
      x = 0;
      break;
    case 'e':
      x = (*y).length();
      break;
    case 'p':
      x = 0;
      if (y != s.begin())
        --y;
      break;
    case 'n':
      x = 0;
      ++y;
      if (y == s.end())
        --y;
      break;
    case 'f':
      if (x == (int)(*y).length()) {
        ++y;
        if (y == s.end())
          --y;
        else
          x = 0;
      } else {
        ++x;
      }
      break;
    case 'b':
      if (x) {
        --x;
      } else {
        if (y != s.begin()) {
          --y;
          x = (*y).length();
        }
      }
      break;
    case 'd':
      if (x == (int)(*y).length()) {
        list<string>::iterator it = y;
        ++it;
        if (it != s.end()) {
          (*y) += (*it);
          s.erase(it);
        }
      } else {
        (*y).erase(x, 1);
      }
      break;
    case 'k':
      if (x == (int)(*y).length()) {
        list<string>::iterator it = y;
        ++it;
        if (it != s.end()) {
          (*y) += (*it);
          s.erase(it);
          b = "\\n";
        }
      } else {
        b = (*y).substr(x);
        (*y).erase(x);
      }
      break;
    case 'y':
      if (0) {
      } else if (b == "") {
      } else if (b == "\\n") {
        string u = (*y).substr(x);
        (*y).erase(x);
        ++y;
        y = s.insert(y, u);
        x = 0;
      } else {
        (*y).insert(x, b);
        x += b.length();
      }
      break;
    }
  }
  for (y = s.begin(); y != s.end(); ++y)
    cout << *y << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <string>
#include <vector>
using namespace std;
int main() {
  int p = 0;
  char c[32] = "";
  string s, t = "";
  while (strcmp(c, "END_OF_TEXT\\n")) {
    s += c;
    fgets(c, 32, stdin);
  }
  s.erase(s.length() - 1);
  while (cin >> c[0] && c[0] != '-') {
    switch (c[0]) {
    case 'a':
      if (p) {
        for (; p > 0; p--) {
          if (s[p - 1] == '\\n')
            break;
        }
      }
      break;
    case 'e':
      p = s.find('\\n', p) < 0 ? s.length() : s.find('\\n', p);
      break;
    case 'p':
      if (p) {
        for (; p > 0; p--) {
          if (s[p - 1] == '\\n')
            break;
        }
        if (p) {
          for (p--; p > 0; p--) {
            if (s[p - 1] == '\\n')
              break;
          }
        }
      }
      break;
    case 'n':
      p = s.find('\\n', p) < 0 ? s.length() : s.find('\\n', p);
      if (p == (int)s.length()) {
        for (; p > 0; p--) {
          if (s[p - 1] == '\\n')
            break;
        }
      } else
        p++;
      break;
    case 'f':
      if (p != (int)s.length())
        p++;
      break;
    case 'b':
      if (p != 0)
        p--;
      break;
    case 'd':
      if (p != (int)s.length())
        s.erase(p, 1);
      break;
    case 'k':
      if (p != (int)s.length()) {
        if (s[p] == '\\n') {
          t = s[p];
          s.erase(p, 1);
        } else {
          t = s.substr(p, s.find('\\n', p) - p);
          s.erase(p, s.find('\\n', p));
        }
      }
      break;
    case 'y':
      s.insert(p, t);
      p += t.length();
      break;
    }
  }
  cout << s << endl;
  return 0;
}