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

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

using namespace std;

vector<string> text;
string buf;

int row, col;

void func_a() { col = 0; }

void func_e() { col = text[row].size(); }

void func_p() {
  if (row > 0)
    row--;
  col = 0;
}

void func_n() {
  if (row < text.size() - 1)
    row++;
  col = 0;
}

void func_f() {
  if (col < text[row].size())
    col++;
  else if (row < text.size() - 1) {
    col = 0;
    row++;
  }
}

void func_b() {
  if (col > 0)
    col--;
  else if (row > 0) {
    row--;
    col = text[row].size();
  }
}

void func_d() {
  if (col < text[row].size())
    text[row].erase(col, 1);
  else if (row < text.size() - 1) {
    text[row] += text[row + 1];
    text.erase(text.begin() + row + 1);
  }
}

void func_k() {
  if (col < text[row].size()) {
    buf = text[row].substr(col, text[row].size() - col);
    text[row] = text[row].substr(0, col);
  } else if (row < text.size() - 1) {
    func_d();
    buf = "\\n";
  }
}

void func_y() {
  if (!buf.empty()) {
    if (buf == "\\n") {
      text.insert(text.begin() + row + 1,
                  text[row].substr(col, text[row].size() - col));
      text[row].erase(col, text[row].size() - 1);
      row++;
      col = 0;
    } else {
      text[row].insert(col, buf);
      col += buf.size();
    }
  }
}

int main() {
  string in;
  char cmd;

  while (getline(cin, in), in != "END_OF_TEXT") {
    text.push_back(in);
  }

  col = row = 0;
  buf.clear();

  while (cin >> cmd, cmd != '-') {
    switch (cmd) {
    case 'a':
      func_a();
      break;
    case 'e':
      func_e();
      break;
    case 'p':
      func_p();
      break;
    case 'n':
      func_n();
      break;
    case 'f':
      func_f();
      break;
    case 'b':
      func_b();
      break;
    case 'd':
      func_d();
      break;
    case 'k':
      func_k();
      break;
    case 'y':
      func_y();
      break;
    }
#if 0
		printf("col:%d row:%d\\n", col, row);
		puts("status:");
		for (vector<string>::iterator it = text.begin(); it != text.end(); it++){
			cout << *it << endl;
		}
		puts("buffer:");
		cout << buf << endl;
#endif
  }

  for (vector<string>::iterator it = text.begin(); it != text.end(); it++) {
    cout << *it << endl;
  }
  return (0);
}

Error: 0

Fixed submission:

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

using namespace std;

vector<string> text;
string buf;

int row, col;

void func_a() { col = 0; }

void func_e() { col = text[row].size(); }

void func_p() {
  if (row > 0)
    row--;
  func_a();
}

void func_n() {
  if (row < text.size() - 1)
    row++;
  func_a();
}

void func_f() {
  if (col < text[row].size())
    col++;
  else if (row < text.size() - 1) {
    func_n();
  }
}

void func_b() {
  if (col > 0)
    col--;
  else if (row > 0) {
    func_p();
    func_e();
  }
}

void func_d() {
  if (col < text[row].size())
    text[row].erase(col, 1);
  else if (row < text.size() - 1) {
    text[row].erase(text[row].end() - 1);
    text[row] += text[row + 1];
    text.erase(text.begin() + row + 1);
  }
}

void func_k() {
  if (col < text[row].size()) {
    buf = text[row].substr(col, text[row].size() - col);
    text[row].erase(col, text[row].size());
  } else if (row < text.size() - 1) {
    func_d();
    buf = "\\n";
  }
}

void func_y() {
  if (!buf.empty()) {
    if (buf == "\\n") {
      text.insert(text.begin() + row + 1, text[row]);
      text[row].erase(col, text[row].size() - 1);
      func_n();
    } else {
      text[row].insert(col, buf);
      col += buf.size();
    }
  }
}

int main() {
  string in;
  char cmd;

  while (getline(cin, in), in != "END_OF_TEXT") {
    text.push_back(in);
  }

  col = row = 0;
  buf.clear();

  while (cin >> cmd, cmd != '-') {
    switch (cmd) {
    case 'a':
      func_a();
      break;
    case 'e':
      func_e();
      break;
    case 'p':
      func_p();
      break;
    case 'n':
      func_n();
      break;
    case 'f':
      func_f();
      break;
    case 'b':
      func_b();
      break;
    case 'd':
      func_d();
      break;
    case 'k':
      func_k();
      break;
    case 'y':
      func_y();
      break;
    }
#if 0
		printf("col:%d row:%d\\n", col, row);
		puts("status:");
		for (vector<string>::iterator it = text.begin(); it != text.end(); it++){
			cout << *it << endl;
		}
		puts("buffer:");
		cout << buf << endl;
#endif
  }

  for (vector<string>::iterator it = text.begin(); it != text.end(); it++) {
    cout << *it << endl;
  }
  return (0);
}