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

#include <algorithm>
#include <math.h>
#include <stack>
#include <stdio.h>
using namespace std;

int main() {

  char buf[200];
  stack<double> S;
  double tmp, op1, op2;
  bool numFLG, negFLG;

  while (fgets(buf, 200, stdin) != 0) {
    numFLG = false;
    negFLG = false;
    tmp = 0;
    for (int i = 0; buf[i] != '\\n' && buf[i] != '\\0'; i++) {
      if (buf[i] >= '0' && buf[i] <= '9') {
        numFLG = true;
        tmp = 10 * tmp + (buf[i] - '0');

        if (buf[i + 1] == '\\n' || buf[i + 1] == '\\0') {
          if (negFLG) {
            tmp = -1 * tmp;
            negFLG = false;
          }
          S.push(tmp);
        }

      } else {
        if (numFLG) {
          numFLG = false;
          if (negFLG) {
            tmp = -1 * tmp;
            negFLG = false;
          }
          S.push(tmp);
          tmp = 0;
        }

        if (buf[i] == ' ') {
          // Do nothing
        } else if (buf[i] == '+') {
          op2 = S.top();
          S.pop();
          op1 = S.top();
          S.pop();
          S.push(op1 + op2);
        } else if (buf[i] == '-') {
          if (buf[i + 1] != '\\n' && buf[i + 1] >= '0' && buf[i + 1] <= '9') {
            negFLG = true;
          } else {
            op2 = S.top();
            S.pop();
            op1 = S.top();
            S.pop();
            S.push(op1 - op2);
          }
        } else if (buf[i] == '*') {
          op2 = S.top();
          S.pop();
          op1 = S.top();
          S.pop();
          S.push(op1 * op2);
        } else if (buf[i] == '/') {
          op2 = S.top();
          S.pop();
          op1 = S.top();
          S.pop();
          S.push(op1 / op2);
        }
      }
    }

    printf("%.6lf\\n", S.top());
    S.pop();
  }

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <math.h>
#include <stack>
#include <stdio.h>
using namespace std;

int main() {

  char buf[200];
  stack<double> S;
  double tmp, op1, op2;
  bool numFLG, negFLG;

  while (fgets(buf, 200, stdin) != 0) {
    numFLG = false;
    negFLG = false;
    tmp = 0;
    for (int i = 0; buf[i] != '\\n' && buf[i] != '\\0'; i++) {
      if (buf[i] >= '0' && buf[i] <= '9') {
        numFLG = true;
        tmp = 10 * tmp + (buf[i] - '0');

        if (buf[i + 1] == '\\n' || buf[i + 1] == '\\0') {
          if (negFLG) {
            tmp = -1 * tmp;
            negFLG = false;
          }
          S.push(tmp);
        }

      } else {
        if (numFLG) {
          numFLG = false;
          if (negFLG) {
            tmp = -1 * tmp;
            negFLG = false;
          }
          S.push(tmp);
          tmp = 0;
        }

        if (buf[i] == ' ') {
          // Do nothing
        } else if (buf[i] == '+') {
          op2 = S.top();
          S.pop();
          op1 = S.top();
          S.pop();
          S.push(op1 + op2);
        } else if (buf[i] == '-') {
          if (i == 0 || (i >= 2 && (buf[i - 2] == '+' || buf[i - 2] == '-' ||
                                    buf[i - 2] == '*' || buf[i - 2] == '/'))) {
            negFLG = true;
          } else {
            op2 = S.top();
            S.pop();
            op1 = S.top();
            S.pop();
            S.push(op1 - op2);
          }
        } else if (buf[i] == '*') {
          op2 = S.top();
          S.pop();
          op1 = S.top();
          S.pop();
          S.push(op1 * op2);
        } else if (buf[i] == '/') {
          op2 = S.top();
          S.pop();
          op1 = S.top();
          S.pop();
          S.push(op1 / op2);
        }
      }
    }

    printf("%.6lf\\n", S.top());
    S.pop();
  }

  return 0;
}