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

#include <iomanip>
#include <iostream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int index(char c) { return (int)c - (int)'0'; }
int main() {
  string s;
  while (getline(cin, s)) {
    double num = 0;
    double a, b;
    stack<double> si;
    string::iterator iter = s.begin();
    while (iter != s.end()) {
      if ((*iter) == '+') {
        a = si.top();
        si.pop();
        b = si.top();
        si.pop();
        num = a + b;
        si.push(num);
      } else if (((*iter) == '-') &&
                 (iter + 1 == s.end() || !isdigit(iter[1]))) {
        a = si.top();
        si.pop();
        b = si.top();
        si.pop();
        num = b - a;
        si.push(num);
      } else if (((*iter) == '-') && (iter + 1 != s.end()) &&
                 isdigit(*(++iter))) {
        vector<double> vd;
        vd.push_back(index(*iter));
        while (++iter != s.end() && isdigit(*iter)) {
          vd.push_back(index(*iter));
        }
        double c = 0;
        double n = 1;
        for (vector<double>::reverse_iterator iter = vd.rbegin();
             iter != vd.rend(); iter++) {
          c += (*iter) * n;
          n = n * 10;
        }
        c = c * -1;
        num = c;
        si.push(c);
      } else if ((*iter) == '*') {
        a = si.top();
        si.pop();
        b = si.top();
        si.pop();
        num = a * b;
        si.push(num);
      } else if ((*iter) == '/') {
        a = si.top();
        si.pop();
        b = si.top();
        si.pop();
        num = b / a;
        si.push(num);
      } else if (isdigit(*iter)) {
        vector<double> vd;
        vd.push_back(index(*iter));
        while (++iter != s.end() && isdigit(*iter)) {
          vd.push_back(index(*iter));
        }
        double c = 0;
        double n = 1;
        for (vector<double>::reverse_iterator iter = vd.rbegin();
             iter != vd.rend(); iter++) {
          c += (*iter) * n;
          n = n * 10;
        }
        num = c;
        si.push(c);
      }
      if (iter != s.end())
        iter++;
    }
    cout << fixed << setprecision(6) << num << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iomanip>
#include <iostream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int index(char c) { return (int)c - (int)'0'; }
int main() {
  string s;
  while (getline(cin, s)) {
    double num = 0;
    double a, b;
    stack<double> si;
    string::iterator iter = s.begin();
    while (iter != s.end()) {
      if ((*iter) == '+') {
        a = si.top();
        si.pop();
        b = si.top();
        si.pop();
        num = a + b;
        si.push(num);
      } else if (((*iter) == '-') &&
                 (iter + 1 == s.end() || !isdigit(iter[1]))) {
        a = si.top();
        si.pop();
        b = si.top();
        si.pop();
        num = b - a;
        si.push(num);
      } else if (((*iter) == '-') && (iter + 1 != s.end()) &&
                 isdigit(*(++iter))) {
        vector<double> vd;
        vd.push_back(index(*iter));
        while (isdigit(*(++iter))) {
          vd.push_back(index(*iter));
        }
        double c = 0;
        double n = 1;
        for (vector<double>::reverse_iterator iter = vd.rbegin();
             iter != vd.rend(); iter++) {
          c += (*iter) * n;
          n = n * 10;
        }
        c = c * -1;
        si.push(c);
      } else if ((*iter) == '*') {
        a = si.top();
        si.pop();
        b = si.top();
        si.pop();
        num = a * b;
        si.push(num);
      } else if ((*iter) == '/') {
        a = si.top();
        si.pop();
        b = si.top();
        si.pop();
        num = b / a;
        si.push(num);
      } else if (isdigit(*iter)) {
        vector<double> vd;
        vd.push_back(index(*iter));
        while (isdigit(*(++iter))) {
          vd.push_back(index(*iter));
        }
        double c = 0;
        double n = 1;
        for (vector<double>::reverse_iterator iter = vd.rbegin();
             iter != vd.rend(); iter++) {
          c += (*iter) * n;
          n = n * 10;
        }
        si.push(c);
      }
      iter++;
    }
    cout << fixed << setprecision(6) << num << endl;
  }
  return 0;
}