hey there! ok i need help figuring out what is wrong with my code. i have to write a program that does addition and subtraction in stacking. but all im getting is error. some advice would be greatly appreciated thanks

here is the code

#include <iostream>
#include <stack>
#include <string>
#include <cmath>
using namespace std;

int main()
{       stack <int> s;
        int op1, op2;
        string str;
        int i;

        cout<<"enter a post fix expression: ";
        getline(cin, str);

        for(i=0;i<str.length();i++)
        {       if(isdigit(str[i]))
                {       s.push(str[i]);
                }else
                {       char ch = str[i];
                        switch(ch);
                        {       case '+': //addition
                                        op1=s.top();
                                        s.pop();
                                        op2=s.top();
                                        s.pop();
                                        s.push(op1+op2);
                                        break;
                                case '-': //substract
                                        op1=s.top();
                                        s.pop();
                                        op2=s.top();
                                        s.pop();
                                        s.push(op2-op1);
                                        break;
                                case 'q': //quits the calculator
                                         break;
                        }
                }
        }
                if(!empty())
                {       cout<<"the answer is: s.top()"<<endl;
                }else
                {       cout<<"error";
                }

return 0;
}

Dani AI

Generated

Here are concise, practical fixes and tips that address the compilation and runtime problems visible in the posted code and replies from and .

The likely root causes

  • A stray semicolon after the switch header (e.g. switch(ch);) makes all case labels invalid. Remove the semicolon so the case labels are inside the switch.
  • Pushing char values directly gives ASCII codes, not numeric values. For single digits convert with ch - '0' or parse whole numbers before pushing.
  • Use the stack object when testing emptiness: call s.empty() or s.size() (not empty() alone).
  • Always check there are enough operands before calling top()/pop() to avoid underflow.
  • The cout line should print the value returned by s.top() (not a quoted literal).

Safe pop/operate pattern

if (s.size() < 2) { cerr << "error: not enough operands\n"; break; }
int b = s.top(); s.pop();
int a = s.top(); s.pop();
s.push(a + b); // or s.push(a - b);

Parsing digits robustly (handles multi-digit numbers)

int val = 0;
while (i < str.length() && isdigit((unsigned char)str[i])) {
  val = val * 10 + (str[i] - '0');
  ++i;
}
s.push(val);
--i; // compensate for the outer loop increment

Additional notes

  • Prefer stack<int> for numeric calculations. Use isdigit((unsigned char)ch) to avoid UB on signed char.
  • Trim or skip spaces between tokens instead of treating spaces as operators.
  • These checks explain the compile errors shown earlier and are the usual fixes; ’s hint about types was on target.

Recommended Answers

All 3 Replies

throw in some output statements within your code to see what top is after each push to be sure you are actually putting something in the stack. I'm concerned that you declare the stack using type int but you are trying to push type char onto the stack. That also means you are trying to do math with type char. This might work, as char are ints per a character set someplace in the background, but I'm not sure it will. This is how I'd find out. If you like using a debugger to follow variable values through a program instead of sprinkling output statements in, by all means do so.

ok i took ur advice an put some output statements an changed the <int> to <char> but the error message is the same this is what it says....


calculator.cpp: In function âint main()â:
calculator.cpp:22: error: case label â'+'â not within a switch statement
calculator.cpp:30: error: case label â'-'â not within a switch statement
calculator.cpp:38: error: case label â'q'â not within a switch statement
calculator.cpp:43: error: âemptyâ was not declared in this scope

ok nevermind i figured it out thanks for the help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.