Problem p00429 (C++)

Buggy submission (status: Runtime Error):
#include <stack>
#include <stdio.h>

using namespace std;

int main() {
  char buf1[1000], buf2[1000], pre;
  int n, p, count, tmp;
  stack<int> S;

  while (true) {
    scanf("%d", &n);
    if (n == 0)
      break;
    scanf("%s", buf1);

    for (int i = 0; i < n; i++) {
      p = count = 0;
      if (i % 2 == 0) {
        for (int k = 0; buf1[k] != '\0';) {
          if (k == 0) {
            pre = buf1[k];
            count++;
            k++;
          } else {
            if (pre == buf1[k]) {
              if (buf1[k + 1] != '\0') {
                count++;
              } else {
                count++;
                if (count >= 10) {
                  while (count != 0) {
                    S.push(count % 10);
                    count /= 10;
                  }
                  while (!S.empty()) {
                    tmp = S.top();
                    S.pop();
                    buf2[p++] = '0' + tmp;
                  }
                  buf2[p++] = pre;
                  buf2[p++] = '\0';

                } else {
                  buf2[p++] = '0' + count;
                  buf2[p++] = pre;
                  buf2[p++] = '\0';
                }
              }
              k++;
            } else {
              if (count >= 10) {
                while (count != 0) {
                  S.push(count % 10);
                  count /= 10;
                }
                while (!S.empty()) {
                  tmp = S.top();
                  S.pop();
                  buf2[p++] = '0' + tmp;
                }
                buf2[p++] = pre;
              } else {
                buf2[p++] = '0' + count;
                buf2[p++] = pre;
              }
              pre = buf1[k];
              count = 0;
            }
          }
        }
      } else {
        for (int k = 0; buf2[k] != '\0';) {
          if (k == 0) {
            pre = buf2[k];
            count++;
            k++;
          } else {
            if (pre == buf2[k]) {
              if (buf2[k + 1] != '\0') {
                count++;
              } else {
                count++;
                if (count >= 10) {
                  while (count != 0) {
                    S.push(count % 10);
                    count /= 10;
                  }
                  while (!S.empty()) {
                    tmp = S.top();
                    S.pop();
                    buf1[p++] = '0' + tmp;
                  }
                  buf1[p++] = pre;
                  buf1[p++] = '\0';

                } else {
                  buf1[p++] = '0' + count;
                  buf1[p++] = pre;
                  buf1[p++] = '\0';
                }
              }
              k++;
            } else {
              if (count >= 10) {
                while (count != 0) {
                  S.push(count % 10);
                  count /= 10;
                }
                while (!S.empty()) {
                  tmp = S.top();
                  S.pop();
                  buf1[p++] = '0' + tmp;
                }
                buf1[p++] = pre;
              } else {
                buf1[p++] = '0' + count;
                buf1[p++] = pre;
              }
              pre = buf2[k];
              count = 0;
            }
          }
        }
      }
    }

    if (n % 2 == 1)
      printf("%s\n", buf2);
    else {
      printf("%s\n", buf1);
    }
  }

  return 0;
}

Fixed submission:
#include <stack>
#include <stdio.h>

using namespace std;

int main() {
  char buf1[100000], buf2[100000], pre;
  int n, p, count, tmp;
  stack<int> S;

  while (true) {
    scanf("%d", &n);
    if (n == 0)
      break;
    scanf("%s", buf1);

    for (int i = 0; i < n; i++) {
      p = count = 0;
      if (i % 2 == 0) {
        for (int k = 0; buf1[k] != '\0';) {
          if (k == 0) {
            pre = buf1[k];
            count++;
            k++;
          } else {
            if (pre == buf1[k]) {
              if (buf1[k + 1] != '\0') {
                count++;
              } else {
                count++;
                if (count >= 10) {
                  while (count != 0) {
                    S.push(count % 10);
                    count /= 10;
                  }
                  while (!S.empty()) {
                    tmp = S.top();
                    S.pop();
                    buf2[p++] = '0' + tmp;
                  }
                  buf2[p++] = pre;
                  buf2[p++] = '\0';

                } else {
                  buf2[p++] = '0' + count;
                  buf2[p++] = pre;
                  buf2[p++] = '\0';
                }
              }
              k++;
            } else {
              if (count >= 10) {
                while (count != 0) {
                  S.push(count % 10);
                  count /= 10;
                }
                while (!S.empty()) {
                  tmp = S.top();
                  S.pop();
                  buf2[p++] = '0' + tmp;
                }
                buf2[p++] = pre;
              } else {
                buf2[p++] = '0' + count;
                buf2[p++] = pre;
              }
              pre = buf1[k];
              count = 0;
            }
          }
        }
      } else {
        for (int k = 0; buf2[k] != '\0';) {
          if (k == 0) {
            pre = buf2[k];
            count++;
            k++;
          } else {
            if (pre == buf2[k]) {
              if (buf2[k + 1] != '\0') {
                count++;
              } else {
                count++;
                if (count >= 10) {
                  while (count != 0) {
                    S.push(count % 10);
                    count /= 10;
                  }
                  while (!S.empty()) {
                    tmp = S.top();
                    S.pop();
                    buf1[p++] = '0' + tmp;
                  }
                  buf1[p++] = pre;
                  buf1[p++] = '\0';

                } else {
                  buf1[p++] = '0' + count;
                  buf1[p++] = pre;
                  buf1[p++] = '\0';
                }
              }
              k++;
            } else {
              if (count >= 10) {
                while (count != 0) {
                  S.push(count % 10);
                  count /= 10;
                }
                while (!S.empty()) {
                  tmp = S.top();
                  S.pop();
                  buf1[p++] = '0' + tmp;
                }
                buf1[p++] = pre;
              } else {
                buf1[p++] = '0' + count;
                buf1[p++] = pre;
              }
              pre = buf2[k];
              count = 0;
            }
          }
        }
      }
    }

    if (n % 2 == 1)
      printf("%s\n", buf2);
    else {
      printf("%s\n", buf1);
    }
  }

  return 0;
}