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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int main() {

  string str;
  while (1) {
    if (cin.eof())
      break;
    getline(cin, str);
    if (str == "")
      break;
    stack<double> s;

    for (int i = 0; i < str.size(); i++) {
      if (str[i] == '*') {
        double n1 = s.top();
        s.pop();
        double n2 = s.top();
        s.pop();
        s.push(n1 * n2);
      } else if (str[i] == '+') {
        double n1 = s.top();
        s.pop();
        double n2 = s.top();
        s.pop();
        s.push(n1 + n2);
      } else if (str[i] == '/') {
        double n1 = s.top();
        s.pop();
        double n2 = s.top();
        s.pop();
        s.push(n2 / n1);
      } else if (str[i] == '-') {
        if (i != str.size() - 1 && str[i + 1] >= '0' && str[i + 1] <= '9') {
          for (int j = i + 1; j <= str.size(); j++) {
            if (j != str.size() && (str[j] >= '0' && str[j] <= '9')) {

            } else {
              double num = atoi(str.substr(i, j - i).c_str());
              s.push(num);
              i = j;
              break;
            }
          }
        } else {
          double n1 = s.top();
          s.pop();
          double n2 = s.top();
          s.pop();
          s.push(-n1 + n2);
        }
      } else if (str[i] >= '0' && str[i] <= '9') {
        for (int j = i + 1; j <= str.size(); j++) {
          if (j != str.size() && (str[j] >= '0' && str[j] <= '9')) {

          } else {
            double num = atoi(str.substr(i, j - i).c_str());
            s.push(num);
            i = j;
            break;
          }
        }
      }
    }
    double d = s.top();
    printf("%.7f\\n", d);
  }

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int main() {

  string str;
  while (1) {
    if (cin.eof())
      break;
    getline(cin, str);
    if (str == "")
      break;
    stack<double> s;
    for (int i = 0; i < str.size(); i++) {
      if (str[i] == '*') {
        double n1 = s.top();
        s.pop();
        double n2 = s.top();
        s.pop();
        s.push(n1 * n2);
      } else if (str[i] == '+') {
        double n1 = s.top();
        s.pop();
        double n2 = s.top();
        s.pop();
        s.push(n1 + n2);
      } else if (str[i] == '/') {
        double n1 = s.top();
        s.pop();
        double n2 = s.top();
        s.pop();
        s.push(n2 / n1);
      } else if (str[i] == '-') {
        double n1 = s.top();
        s.pop();
        double n2 = s.top();
        s.pop();
        s.push(-n1 + n2);
      } else if (str[i] >= '0' && str[i] <= '9') {
        for (int j = i + 1; j <= str.size(); j++) {
          if (j != str.size() && (str[j] >= '0' && str[j] <= '9')) {

          } else {
            if (j - 1 >= str.size()) {
              s.push(10);
              i = str.size();
              break;
            } else {
              double num = atoi(str.substr(i, j - i).c_str());
              s.push(num);
              i = j;
              break;
            }
          }
        }
      }
    }
    double d = s.top();
    printf("%.7f\\n", d);
  }

  return 0;
}