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

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <stack>
#include <string>
using namespace std;
char ch[100];
string str;
int main() {
  stack<double> std;
  int i, c, j, size;
  bool flag = false;
  double a, b;
  while (getline(cin, str)) {
    c = 0;
    size = str.size();
    if (str[size - 1] != '+' && str[size - 1] != '-' && str[size - 1] != '/' &&
        str[size - 1] != '*') {
      for (i = 0; i < size; i++) {
        ch[c] = str[i];
        c++;
      }
      std.push(atof(ch));
      c = 0;
    } else {
      for (i = 0; i < size; i++) {
        if (str[i] != ' ' && str[i] != '\\0') {
          if (str[i] == '+') {
            a = std.top();
            std.pop();
            b = std.top();
            std.pop();
            std.push(a + b);
            flag = true;
          } else if (str[i] == '/') {
            a = std.top();
            std.pop();
            b = std.top();
            std.pop();
            std.push(b / a);
            flag = true;
          } else if (str[i] == '*') {
            a = std.top();
            std.pop();
            b = std.top();
            std.pop();
            std.push(a * b);
            flag = true;
          } else if (str[i] == '-') {
            if (str[i + 1] == ' ' || str[i + 1] == '\\0') {
              a = std.top();
              std.pop();
              b = std.top();
              std.pop();
              std.push(b - (a));
              flag = true;
            } else {
              ch[c] = str[i];
              c++;
              flag = false;
            }
          } else {
            ch[c] = str[i];
            c++;
            flag = false;
          }
        } else {
          if (i + 1 < size) {
            if (!flag)
              std.push(atof(ch));
            for (j = 0; j < c; j++)
              ch[j] = '\\0';
            c = 0;
          }
        }
      }
    }
    printf("%.6f\\n", std.top());
    std.pop();
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <stack>
#include <string>
using namespace std;
char ch[100];
string str;
int main() {
  stack<double> std;
  int i, c, j;
  bool flag = false;
  double a, b;
  while (getline(cin, str)) {
    c = 0;
    for (i = 0; i <= str.size(); i++) {
      if (str[i] != ' ' && str[i] != '\\0') {
        if (str[i] == '+') {
          a = std.top();
          std.pop();
          b = std.top();
          std.pop();
          std.push(a + b);
          flag = true;
        } else if (str[i] == '/') {
          a = std.top();
          std.pop();
          b = std.top();
          std.pop();
          std.push(b / a);
          flag = true;
        } else if (str[i] == '*') {
          a = std.top();
          std.pop();
          b = std.top();
          std.pop();
          std.push(a * b);
          flag = true;
        } else if (str[i] == '-') {
          if (str[i + 1] == ' ') {
            a = std.top();
            std.pop();
            b = std.top();
            std.pop();
            std.push(b - a);
            flag = true;
          } else {
            ch[c] = str[i];
            c++;
            flag = false;
          }
        } else {
          ch[c] = str[i];
          c++;
          flag = false;
        }
      } else {
        if (i + 1 < str.size()) {
          if (!flag)
            std.push(atof(ch));
          for (j = 0; j < c; j++)
            ch[j] = '\\0';
          c = 0;
        }
      }
    }
    printf("%.6f\\n", std.top());
    std.pop();
  }
  return 0;
}