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

#include <iostream>
#include <string>
using namespace std;
string s;
string color() {
  char l[6];
  for (int i = 0; i < 6; i++) {
    l[i] = s[i + 1];
  }
  int r, g, b;
  int z[6];
  for (int i = 0; i < 6; i++) {
    if (l[i] == '0') {
      z[i] = 0;
    } else if (l[i] == '1') {
      z[i] = 1;
    } else if (l[i] == '2') {
      z[i] = 2;
    } else if (l[i] == '3') {
      z[i] = 3;
    } else if (l[i] == '4') {
      z[i] = 4;
    } else if (l[i] == '5') {
      z[i] = 5;
    } else if (l[i] == '6') {
      z[i] = 6;
    } else if (l[i] == '7') {
      z[i] = 7;
    } else if (l[i] == '8') {
      z[i] = 8;
    } else if (l[i] == '9') {
      z[i] = 9;
    } else if (l[i] == 'a') {
      z[i] = 10;
    } else if (l[i] == 'b') {
      z[i] = 11;
    } else if (l[i] == 'c') {
      z[i] = 12;
    } else if (l[i] == 'd') {
      z[i] = 13;
    } else if (l[i] == 'e') {
      z[i] = 14;
    } else if (l[i] == 'f') {
      z[i] = 15;
    }
  }
  r = z[0] * 16 + z[1];
  g = z[2] * 16 + z[3];
  b = z[4] * 16 + z[5];
  if (r <= 127) {
    if (g <= 127) {
      if (b <= 127) {
        return "black";
      } else {
        return "blue";
      }
    } else if (b <= 127) {
      return "lime";
    } else {
      return "aqua";
    }
  } else if (g <= 127) {
    if (b <= 127) {
      return "red";
    } else {
      return "fuchsia";
    }
  } else if (b <= 127) {
    return "yellow";
  } else {
    return "white";
  }
}

int main() {
  string g[1001];
  int x;
  for (int i = 0;; i++) {
    cin >> s;
    if (s == "0") {
      x = i;
      break;
    }
    g[i] = color();
  }
  for (int i = 0; i < x; i++) {
    cout << g[i] << endl;
  }
  char c;
  cin >> c;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <string>
using namespace std;

char l[6];
string s;

string color() {
  for (int i = 0; i < 6; i++) {
    l[i] = s[i + 1];
  }
  int r, g, b;
  int z[6];

  for (int i = 0; i < 6; i++) {
    if (l[i] == '0') {
      z[i] = 0;
    } else if (l[i] == '1') {
      z[i] = 1;
    } else if (l[i] == '2') {
      z[i] = 2;
    } else if (l[i] == '3') {
      z[i] = 3;
    } else if (l[i] == '4') {
      z[i] = 4;
    } else if (l[i] == '5') {
      z[i] = 5;
    } else if (l[i] == '6') {
      z[i] = 6;
    } else if (l[i] == '7') {
      z[i] = 7;
    } else if (l[i] == '8') {
      z[i] = 8;
    } else if (l[i] == '9') {
      z[i] = 9;
    } else if (l[i] == 'a') {
      z[i] = 10;
    } else if (l[i] == 'b') {
      z[i] = 11;
    } else if (l[i] == 'c') {
      z[i] = 12;
    } else if (l[i] == 'd') {
      z[i] = 13;
    } else if (l[i] == 'e') {
      z[i] = 14;
    } else if (l[i] == 'f') {
      z[i] = 15;
    }
  }

  r = z[0] * 16 + z[1];
  g = z[2] * 16 + z[3];
  b = z[4] * 16 + z[5];

  // ff == 255;
  if (r <= 128) {
    // black,blue,lime,aqua
    if (g <= 128) {
      // black,blue
      if (b <= 128) {
        return "black";
      } else {
        return "blue";
      }
    } else {
      // lime,aqua
      if (b <= 128) {
        return "lime";
      } else {
        return "aqua";
      }
    }
  } else {
    // red,fuchsia,yellow,white
    if (g <= 128) {
      // red,fu
      if (b <= 128) {
        return "red";
      } else {
        return "fuchsia";
      }
    } else {
      if (b <= 128) {
        return "yellow";
      } else {
        return "white";
      }
    }
  }
}

int main() {
  string g[100];
  int x;
  for (int i = 0;; i++) {
    cin >> s;
    if (s == "0") {
      x = i;
      break;
    }
    g[i] = color();
  }
  for (int i = 0; i < x; i++) {
    cout << g[i] << endl;
  }
  return 0;
}