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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define mp make_pair

template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << *i << " ";
  cout << endl;
}
template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}

stack<double> st;

int main() {
  char str[100];
  char ch;
  double buf1, buf2;
  while (~scanf("%s", str)) {
    if (isdigit(str[0]) ||
        str[0] == '-' && strlen(str) > 1 && isdigit(str[1])) {
      st.push(atof(str));
      // cout<<atof(str)<<endl;
    } else {
      if (str[0] == '+') {
        buf1 = st.top();
        st.pop();
        buf2 = st.top();
        st.pop();
        st.push(buf1 + buf2);
        // cout<<st.top()<<endl;
      }
      if (str[0] == '-') {
        buf1 = st.top();
        st.pop();
        buf2 = st.top();
        st.pop();
        st.push(buf2 - buf1);
        // cout<<st.top()<<endl;
      }
      if (str[0] == '*') {
        buf1 = st.top();
        st.pop();
        buf2 = st.top();
        st.pop();
        st.push(buf1 * buf2);
        // cout<<st.top()<<endl;
      }
      if (str[0] == '/') {
        buf1 = st.top();
        st.pop();
        buf2 = st.top();
        st.pop();
        st.push(buf2 / buf1);
        // cout<<st.top()<<endl;
      }
    }
    ch = getchar();
    if (ch != ' ') {
      buf1 = st.top();
      printf("%.10f\\n", buf1);
      st.pop();
    }
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define mp make_pair

template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << *i << " ";
  cout << endl;
}
template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}

string s;
double st[100];

int main() {
  while (getline(cin, s)) {
    // cout<<s.size()<<endl;
    int i;
    int pos = 0;
    double a = 0;
    bool num = false;
    for (i = 0; i < s.size() - 1; i++) {
      // cout<<pos<<endl;
      // cout<<"  "<<st[0]<<endl;
      // cout<<"  "<<st[1]<<endl;
      // cout<<"  "<<st[2]<<endl;
      if ('0' <= s[i] && s[i] <= '9') {
        a = a * 10 + s[i] - '0';
        num = true;
      } else if (s[i] == ' ') {
        if (num)
          st[pos++] = a;
        a = 0;
        num = false;
      } else if (s[i] == '+') {
        st[pos - 2] = st[pos - 2] + st[pos - 1];
        pos--;
      } else if (s[i] == '-') {
        st[pos - 2] = st[pos - 2] - st[pos - 1];
        pos--;
      } else if (s[i] == '*') {
        st[pos - 2] = st[pos - 2] * st[pos - 1];
        pos--;
      } else if (s[i] == '/') {
        st[pos - 2] = st[pos - 2] / st[pos - 1];
        pos--;
      }
    }
    cout << st[0] << endl;
  }
  return 0;
}