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

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>

using namespace std;

typedef long long ll;

int main() {
  string str;

  map<string, string> encode;
  map<string, string> decode;

  encode[" "] = "101";
  encode["'"] = "000000";
  encode[","] = "000011";
  encode["-"] = "10010001";
  encode["."] = "010001";

  encode["?"] = "000001";
  encode["A"] = "100101";
  encode["B"] = "10011010";
  encode["C"] = "0101";
  encode["D"] = "0001";
  encode["E"] = "110";
  encode["F"] = "01001";
  encode["G"] = "10011011";

  encode["H"] = "010000";
  encode["I"] = "0111";
  encode["J"] = "10011000";
  encode["K"] = "0110";
  encode["L"] = "00100";
  encode["M"] = "10011001";
  encode["N"] = "10011110";

  encode["O"] = "00101";
  encode["P"] = "111";
  encode["Q"] = "10011111";
  encode["R"] = "1000";
  encode["S"] = "00110";
  encode["T"] = "00111";
  encode["U"] = "10011100";

  encode["V"] = "10011101";
  encode["W"] = "000010";
  encode["X"] = "10010010";
  encode["Y"] = "10010011";
  encode["Z"] = "10010000";

  decode["00000"] = "A";
  decode["00001"] = "B";
  decode["00010"] = "C";
  decode["00011"] = "D";
  decode["00100"] = "E";
  decode["00101"] = "F";
  decode["00110"] = "G";

  decode["00111"] = "H";
  decode["01000"] = "I";
  decode["01001"] = "J";
  decode["01010"] = "K";
  decode["01011"] = "L";
  decode["01100"] = "M";
  decode["01101"] = "N";

  decode["01110"] = "O";
  decode["01111"] = "P";
  decode["10000"] = "Q";
  decode["10001"] = "R";
  decode["10010"] = "S";
  decode["10011"] = "T";
  decode["10100"] = "U";

  decode["10101"] = "V";
  decode["10110"] = "W";
  decode["10111"] = "X";
  decode["11000"] = "Y";
  decode["11001"] = "Z";

  decode["11010"] = " ";
  decode["11011"] = ".";
  decode["11100"] = ",";
  decode["11101"] = "-";
  decode["11110"] = "'";
  decode["11111"] = "?";

  while (getline(cin, str)) {

    string enc = "";
    for (int i = 0; i < str.size(); i++) {
      enc += encode[str.substr(i, 1)];
    }

    enc += "0000";

    string dec = "";
    for (int i = 0; i + 5 <= enc.size(); i += 5) {
      dec += decode[enc.substr(i, 5)];
    }

    cout << dec << endl;
  }
}

Error: WA

Fixed submission:

#include <stdio.h>
#include <string.h>

int main(void) {

  char c;
  char code[6];

  int size;
  char buf[500] = "";

  int i, j = 0;

  do {
    // encode
    while ((c = getchar()) != EOF) {

      if (c == '\\n')
        break;

      switch (c) {

      case ' ':
        strcat(buf, "101");
        break;

      case '\\'':
        strcat(buf, "000000");
        break;

      case ',':
        strcat(buf, "000011");
        break;

      case '-':
        strcat(buf, "10010001");
        break;

      case '.':
        strcat(buf, "010001");
        break;

      case '?':
        strcat(buf, "000001");
        break;

      case 'A':
        strcat(buf, "100101");
        break;

      case 'B':
        strcat(buf, "10011010");
        break;

      case 'C':
        strcat(buf, "0101");
        break;

      case 'D':
        strcat(buf, "0001");
        break;

      case 'E':
        strcat(buf, "110");
        break;

      case 'F':
        strcat(buf, "01001");
        break;

      case 'G':
        strcat(buf, "10011011");
        break;

      case 'H':
        strcat(buf, "010000");
        break;

      case 'I':
        strcat(buf, "0111");
        break;

      case 'J':
        strcat(buf, "10011000");
        break;

      case 'K':
        strcat(buf, "0110");
        break;

      case 'L':
        strcat(buf, "00100");
        break;

      case 'M':
        strcat(buf, "10011001");
        break;

      case 'N':
        strcat(buf, "10011110");
        break;

      case 'O':
        strcat(buf, "00101");
        break;

      case 'P':
        strcat(buf, "111");
        break;

      case 'Q':
        strcat(buf, "10011111");
        break;

      case 'R':
        strcat(buf, "1000");
        break;

      case 'S':
        strcat(buf, "00110");
        break;

      case 'T':
        strcat(buf, "00111");
        break;

      case 'U':
        strcat(buf, "10011100");
        break;

      case 'V':
        strcat(buf, "10011101");
        break;

      case 'W':
        strcat(buf, "000010");
        break;

      case 'X':
        strcat(buf, "10010010");
        break;

      case 'Y':
        strcat(buf, "10010011");
        break;

      case 'Z':
        strcat(buf, "10010000");
        break;
      }
    }

    // printf("%s\\n",buf);

    if ((size = strlen(buf) % 5) != 0) {

      for (i = 0; i < 5 - size; i++) {

        strcat(buf, "0");
      }
    }

    // printf("%s\\n",buf);

    // decode
    for (i = 0; i < strlen(buf); i++) {

      code[j] = buf[i];
      j++;

      if (i % 5 == 4) {
        code[5] = '\\0';
        // printf("%s\\n",code);
        j = 0;

        if (strcmp(code, "00000") == 0)
          printf("A");

        if (strcmp(code, "00001") == 0)
          printf("B");

        if (strcmp(code, "00010") == 0)
          printf("C");

        if (strcmp(code, "00011") == 0)
          printf("D");

        if (strcmp(code, "00100") == 0)
          printf("E");

        if (strcmp(code, "00101") == 0)
          printf("F");

        if (strcmp(code, "00110") == 0)
          printf("G");

        if (strcmp(code, "00111") == 0)
          printf("H");

        if (strcmp(code, "01000") == 0)
          printf("I");

        if (strcmp(code, "01001") == 0)
          printf("J");

        if (strcmp(code, "01010") == 0)
          printf("K");

        if (strcmp(code, "01011") == 0)
          printf("L");

        if (strcmp(code, "01100") == 0)
          printf("M");

        if (strcmp(code, "01101") == 0)
          printf("N");

        if (strcmp(code, "01110") == 0)
          printf("O");

        if (strcmp(code, "01111") == 0)
          printf("P");

        if (strcmp(code, "10000") == 0)
          printf("Q");

        if (strcmp(code, "10001") == 0)
          printf("R");

        if (strcmp(code, "10010") == 0)
          printf("S");

        if (strcmp(code, "10011") == 0)
          printf("T");

        if (strcmp(code, "10100") == 0)
          printf("U");

        if (strcmp(code, "10101") == 0)
          printf("V");

        if (strcmp(code, "10110") == 0)
          printf("W");

        if (strcmp(code, "10111") == 0)
          printf("X");

        if (strcmp(code, "11000") == 0)
          printf("Y");

        if (strcmp(code, "11001") == 0)
          printf("Z");

        if (strcmp(code, "11010") == 0)
          printf(" ");

        if (strcmp(code, "11011") == 0)
          printf(".");

        if (strcmp(code, "11100") == 0)
          printf(",");

        if (strcmp(code, "11101") == 0)
          printf("-");

        if (strcmp(code, "11110") == 0)
          printf("'");

        if (strcmp(code, "11111") == 0)
          printf("?");
      }
    }

    puts("");

  } while (c != EOF);

  return 0;
}