1 / 2
Jun 2018

Hello, a newbie here. I’m trying to submit my solution, it gives me a SIGSEGV runtime error, and I’m having a hard time figuring out why, can you help?

code:
#include <iostream>
#include <string>
#nclude <stack>
#include <cassert>

using namespace std;

int main() {
  int t, len, i;
  string s;
  stack<char> op;
  cin >> t;
  assert(t <= 100);
  while (t--) {
    cin >> s;
    len = s.size();
    assert(len <= 400);
    for (i = 0; i < len; i++) {
      if (s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '^')
        op.push(s[i]);
      else if (s[i] == ')'){
        cout << op.top();
        op.pop();
      } else if (isalpha(s[i]))
        cout << s[i];
    }
    cout << "\n";
  }
	return 0;
}

problem link: http://www.spoj.com/problems/ONP/20

  • created

    Jun '18
  • last reply

    Jun '18
  • 1

    reply

  • 1.5k

    views

  • 2

    users

  • 1

    link

I think it’s probably trying to use stack.top or stack.pop when the stack is empty. Try this:

1
(a)

Suggested Topics

Topic Category Replies Views Activity
C and C++ 0 16 9d

Want to read more? Browse other topics in C and C++ or view latest topics.