How do you not pop from an empty stack?

Solved! Thanks!
Last edited on
Your compute() assumes (lines 74-78) that the stack has at least two values in it. Either the caller (i.e. main) must ensure that this is true, and/or the function has to check it self.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
void compute(const string& variable, stack<double>& rpnStack)
{
    double left, right, result;
  
    if( !rpnStack.empty() )
    {
          right = rpnStack.top();
          rpnStack.pop();
    }
    else 
    {
          // stack is empty
          // report error
    }

    if( !rpnStack.empty() )
    {
          left = rpnStack.top();
          rpnStack.pop();
    }
    else 
    {
          // stack is empty
          // report error
    }
    
    // ... 
Topic archived. No new replies allowed.