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

#include <list>
#include <stdio.h>
#include <string.h>
#include <string>
using namespace std;
typedef list<string> L;
typedef L::iterator I;
int main() {
  char str[256];
  L l;
  while (fgets(str, 256, stdin), strcmp(str, "END_OF_TEXT\\n")) {
    str[strlen(str) - 1] = 0;
    l.push_back(str);
  }
  I row = l.begin(), t;
  string::iterator col = row->begin();
  string buf;
  while (fgets(str, 256, stdin), *str - '-') {
    switch (*str) {
    case 'a':
      col = row->begin();
      break;
    case 'e':
      col = row->end();
      break;
    case 'p':
      if (row != l.begin())
        --row;
      col = row->begin();
      break;
    case 'n':
      if (t = l.end(), row != --t)
        ++row;
      col = row->begin();
      break;
    case 'f':
      if (col != row->end())
        ++col;
      else if (t = l.end(), row != --t)
        ++row, col = row->begin();
      break;
    case 'b':
      if (col != row->begin())
        --col;
      else if (row != l.begin())
        --row, col = row->end();
      break;
    case 'd':
      if (col != row->end())
        col = row->erase(col);
      else if (t = l.end(), row != --t) {
        t = row;
        int s = row->size();
        if (++t != l.end()) {
          *row += *t;
          l.erase(t);
        }
        col = row->begin() + s;
      }
      break;
    case 'k':
      if (col != row->end()) {
        buf = row->substr(col - row->begin());
        col = row->erase(col, row->end());
      } else {
        buf = "\\n";
        t = row;
        int s = row->size();
        if (++t != l.end()) {
          *row += *t;
          l.erase(t);
        }
        col = row->begin() + s;
      }
      break;
    case 'y':
      if (buf == "\\n") {
        t = row;
        ++t;
        string s(row->substr(col - row->begin()));
        l.insert(t, s);
        row->erase(col, row->end());
        row = --t;
        col = row->begin();
      } else if (!buf.empty()) {
        int s = col - row->begin() + buf.size();
        row->insert(col - row->begin(), buf);
        col = row->begin() + s;
      }
      break;
    }
  }
  for (t = l.begin(); t != l.end(); ++t)
    printf("%s\\n", t->c_str());
  return 0;
}

Error: 0

Fixed submission:

#include <list>
#include <stdio.h>
#include <string.h>
#include <string>
using namespace std;
typedef list<string> L;
typedef L::iterator I;
int main() {
  char str[256];
  L l;
  while (fgets(str, 256, stdin), strcmp(str, "END_OF_TEXT\\n")) {
    str[strlen(str) - 1] = 0;
    l.push_back(str);
  }
  I row = l.begin(), t;
  string::iterator col = row->begin();
  string buf;
  while (fgets(str, 256, stdin), *str - '-') {
    switch (*str) {
    case 'a':
      col = row->begin();
      break;
    case 'e':
      col = row->end();
      break;
    case 'p':
      if (row != l.begin())
        --row;
      col = row->begin();
      break;
    case 'n':
      if (row != l.end())
        ++row;
      col = row->begin();
      break;
    case 'f':
      if (col != row->end())
        ++col;
      else if (row != l.end())
        ++row, col = row->begin();
      break;
    case 'b':
      if (col != row->begin())
        --col;
      else if (row != l.begin())
        --row, col = row->end();
      break;
    case 'd':
      if (col != row->end())
        col = row->erase(col);
      else if (row != l.end()) {
        t = row;
        int s = row->size();
        ++t;
        *row += *t;
        l.erase(t);
        col = row->begin() + s;
      }
      break;
    case 'k':
      if (col != row->end()) {
        buf = row->substr(col - row->begin());
        col = row->erase(col, row->end());
      } else {
        buf = "\\n";
        t = row;
        int s = row->size();
        ++t;
        *row += *t;
        l.erase(t);
        col = row->begin() + s;
      }
      break;
    case 'y':
      if (buf == "\\n") {
        t = row;
        ++t;
        string s(row->substr(col - row->begin()));
        l.insert(t, s);
        row->erase(col, row->end());
        row = t;
        col = row->begin();
      } else if (!buf.empty()) {
        int s = col - row->begin() + buf.size();
        row->insert(col - row->begin(), buf);
        col = row->begin() + s;
      }
      break;
    }
  }
  for (t = l.begin(); t != l.end(); ++t)
    printf("%s\\n", t->c_str());
  return 0;
}