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

#include <bits/stdc++.h>
using namespace std;

int main() {

  char line[1000], R3;
  int R1, R2;

  while (scanf("%s", line) != EOF) {

    R1 = 0;
    R2 = 0;
    R3 = ' ';

    for (int i = 0; line[i] != '\\0'; i++) {
      if (line[i] >= '0' && line[i] <= '9') {
        R2 = 10 * R2 + (line[i] - '0');
        if (R2 > 9999) {
          printf("E\\n");
          break;
        }
      } else {
        switch (R3) {
        case '+':
          R1 += R2;
          break;
        case '-':
          R1 -= R2;
          break;
        case '*':
          R1 *= R2;
          break;
        case ' ':
          R1 = R2;
          break;
        }

        if (R1 < 0 || R1 > 9999) {
          printf("E\\n");
          break;
        }
        R2 = 0;
        R3 = line[i];
        if (R3 == '=') {
          printf("%d\\n", R1);
          break;
        }
      }
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {

  int tmp, op1, work;

  char line[101], calc;
  stack<int> NUM, work_num;
  stack<char> OP, work_op;

  bool numFLG, breakFLG;

  while (scanf("%s", line) != EOF) {

    tmp = 0;
    numFLG = false;
    breakFLG = false;

    for (int k = 0; line[k] != '\\0'; k++) {

      if (line[k] >= '0' && line[k] <= '9') {
        numFLG = true;
        tmp = 10 * tmp + (line[k] - '0');
        if (tmp > 9999) {
          printf("E\\n");
          break;
        }
      } else {

        if (numFLG == true) {

          if (OP.empty() == false && OP.top() == '*') {
            calc = OP.top();
            OP.pop();
            op1 = NUM.top();
            NUM.pop();

            if (tmp * op1 <= 9999 && tmp * op1 >= 0) {
              NUM.push(tmp * op1);
            } else {
              printf("E\\n");
              break;
            }
          } else {
            NUM.push(tmp);
          }

          numFLG = false;
          tmp = 0;
        }

        if (line[k] == '+' || line[k] == '-' || line[k] == '*' ||
            line[k] == '(') {
          OP.push(line[k]);
        } else { // line[k] == '='

          work = NUM.top();
          NUM.pop();
          work_num.push(work);

          while (!NUM.empty()) {
            calc = OP.top();
            OP.pop();

            work_op.push(calc);

            work = NUM.top();
            NUM.pop();
            work_num.push(work);
          }

          work = work_num.top();
          work_num.pop();

          while (!work_num.empty()) {
            calc = work_op.top();
            work_op.pop();

            if (calc == '+') {
              work += work_num.top();
            } else if (calc == '-') {
              work -= work_num.top();
            } else { // calc == '*'
              work *= work_num.top();
            }
            if (work < 0 || work > 9999) {
              printf("E\\n");
              breakFLG = true;
              break;
            }

            work_num.pop();
          }
          if (breakFLG)
            break;

          if (work >= 0 && work <= 9999) {
            printf("%d\\n", work);
          } else {
            printf("E\\n");
          }
        }
      }
    }
  }

  return 0;
}