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

#include <iostream>
#include <string>

using namespace std;

class Frac {
public:
  int denom, numer;
};

int main() {

  string str;
  Frac frac[25];

  while (cin >> str) {
    if (str[0] == '#')
      break;

    int cnt = 0;
    int pow = 1;

    for (int i = 0; i < str.size(); i++) {
      if (str.substr(i, 4) == "west") {
        frac[cnt].numer = 90;
        i += 3;
      } else {
        frac[cnt].numer = -90;
        i += 4;
      }
      cnt++;
      pow *= 2;
      //	cout<<frac[cnt-1].numer<<"/"<<frac[cnt-1].denom<<endl;
    }
    pow /= 2;
    for (int i = 0; i < cnt; i++) {
      frac[i].denom = pow;
      pow /= 2;
      //	cout<<frac[i].numer<<"/"<<frac[i].denom<<endl;
    }

    Frac a;
    a.denom = frac[cnt - 1].denom;
    a.numer = frac[cnt - 1].numer;

    if (a.numer < 0) {
      a.numer = 0;
      a.denom = 1;
    }
    if (a.numer / a.denom >= 90) {
      a.numer = 90;
      a.denom = 1;
    }

    for (int i = cnt - 2; i >= 0; i--) {
      //	cout<<"i="<<i-1<<" "<<a.numer<<"/"<<a.denom<<endl;
      //	cout<<"frac[i]="<<frac[i].numer<<"/"<<frac[i].denom<<endl;
      if (a.denom != frac[i].denom) {
        int tmpNumer = a.denom;
        a.denom *= frac[i].denom / tmpNumer;
        a.numer *= frac[i].denom / tmpNumer;
      }

      a.numer += frac[i].numer;

      while (1) {
        if (a.denom % 2 == 0 && a.numer % 2 == 0) {
          a.denom /= 2;
          a.numer /= 2;
        } else
          break;
      }
      if (a.numer <= 0) {
        a.numer = 0;
        a.denom = 1;
      }
      if (a.numer / a.denom >= 90) {
        a.numer = 90;
        a.denom = 1;
      }
    }

    cout << a.numer;
    if (a.denom >= 2) {
      cout << "/" << a.denom;
    }
    cout << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <string>

using namespace std;

class Frac {
public:
  int denom, numer;
};

int main() {

  string str;
  Frac frac[25];

  while (cin >> str) {
    if (str[0] == '#')
      break;

    int cnt = 0;
    int pow = 1;

    for (int i = 0; i < str.size(); i++) {
      if (str.substr(i, 4) == "west") {
        frac[cnt].denom = pow;
        frac[cnt].numer = 90;
        i += 3;
      } else {
        frac[cnt].denom = pow;
        frac[cnt].numer = -90;
        i += 4;
      }
      cnt++;
      pow *= 2;
      // cout<<frac[cnt-1].numer<<"/"<<frac[cnt-1].denom<<endl;
    }

    Frac a;
    a.denom = frac[0].denom;
    a.numer = frac[0].numer;

    if (a.numer <= 0) {
      a.numer = 0;
      a.denom = 1;
    }
    if (a.numer / a.denom >= 90) {
      a.numer = 90;
      a.denom = 1;
    }

    for (int i = 1; i < cnt; i++) {
      //	cout<<"i="<<i-1<<" "<<a.numer<<"/"<<a.denom<<endl;
      if (a.numer != frac[i].numer) {
        int tmpNumer = a.denom;
        a.denom *= frac[i].denom;
        a.numer *= frac[i].denom;

        frac[i].numer *= tmpNumer;
        frac[i].denom *= tmpNumer;
      }

      a.numer += frac[i].numer;

      while (1) {
        if (a.denom % 2 == 0 && a.numer % 2 == 0) {
          a.denom /= 2;
          a.numer /= 2;
        } else
          break;
      }
      if (a.numer <= 0) {
        a.numer = 0;
        a.denom = 1;
      }
      if (a.numer / a.denom >= 90) {
        a.numer = 90;
        a.denom = 1;
      }
    }

    cout << a.numer;
    if (a.denom >= 2) {
      cout << "/" << a.denom;
    }
    cout << endl;
  }

  return 0;
}