Assistance with pop function and exception handling

Looking for guidance on how to properly solve his HW problem. I can't seem to correctly use the pop feature or try/catch exception. I have been in contact with my professor and classmates, just looking for some help as many places as I can. Thank you for any clarification. I have looked up the various solutions on this site, as well as the documentation (and tried many solutions) and this is where I have ended up.

-Looks like the lines for errors are -10, just fyi.
--Thank you for the heads up, .h and .cpp are up, (working on line codes)
~Russell

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include <iostream>
#include <string>
using namespace std;


const int MAX = 10;   // The MAX number of elements for the stack
                       // MAX is unknown to the client

typedef char el_t;      // the el_t type is char for now
                      // el_t is unknown to the client


class stack
{

   private: // to be hidden from the client

        el_t     el[MAX];       // el is  an array of el_t's
        int      top;           // top is index to the top element in stack

   public: // prototypes to be used by the client

        // exception handling classes
        class Overflow{};
        class Underflow{};

        stack();   // constructor
        ~stack();  // destructor

        void push(el_t);

        void pop(el_t&);


        void topElem(el_t&);

        bool isEmpty();

        bool isFull();

        void displayAll();

        void clearIt();

};

#include <iostream>
#include <string>
#include "HWstack.h"
using namespace std;

stack::stack()
{
  top = -1; // indicate an empty stack }
}

stack::~stack()
{} 


bool stack::isEmpty()
{
  if (top == -1)
    return true;
  else
    return false;
}

bool stack::isFull()
{ if (top == MAX-1 )
    return true;
  else
    return false;
}

void stack::push(el_t elem)
{ if (isFull())
    throw Overflow();
  else
    top++; el[top] = elem;
}

void stack::pop(el_t& elem)
{ if ( isEmpty() )
    throw Underflow();
  else { elem = el[top]; top--;}
}

void stack::topElem(el_t& elem)
{ if ( isEmpty() )
    throw Underflow();
  else { elem = el[top]; }
}

void stack::displayAll()
{  if (isEmpty())
     cout << "[ empty ]" << endl;
   else
     for (int i=top; i>=0; i--)
     { cout << el[i] << endl; }
}

void stack::clearIt()
{ if (isEmpty())
    cout << "[ empty ]" << endl;
  else
    for (int i=top; i>=0; i--)
    { pop( el[i] ); }
}

#include <iostream>
#include <string>
#include "HWstack.h"
using namespace std;

int main()
{
  stack postfixstack;  // integer stack
  string expression;

  cout << "type a postfix expression: " ;
  cin >> expression;

  int i = 0;  // character position within expression
  char item;

  int box1;  // receive things from pop
  int box2;  // receive things from pop
  int result;// receive integer after operation performed

  while (expression[i] != '\0')
    {
      try
        {
          item = expression[i];    //1.  read an item.

          if ( item == (int(item)) )  //2.  if it is an operand (number),
            {
              int x = int(expression[i]) - 48;

              postfixstack.push(x);//push it (you might get Overflow exception)
              cout<<x<<endl;
              cout<<"--------------"<<endl;  //apply the operator to the two operands
            }
         else if ( (item == '+') || (item == '-') || (item == '*') )//3.  else if it is an operator,
            {
             postfixstack.pop(box1);//pop the two operands
             postfixstack.pop(box2);//(you might get Underflow exception)
             cout<<box2<<endl;
             cout<<box1<<endl;
             cout<<"--------------"<<endl;

             if (item == '+')
               {
                 result = box1+box2;
                 cout<<result<<endl;
                 cout<<"--------------"<<endl;
               }
             else if (item == '-')
               {
                 result = box2-box1;
                 cout<<result<<endl;
                 cout<<"--------------"<<endl;
               }
             else if (item == '*')
               {
                 result = box1*box2;
                 cout<<result<<endl;
                 cout<<"--------------"<<endl;
               }

             postfixstack.push(result);// push the result
            } //close else if
         else throw "invalid item";

        } // this closes try

      // Catch exceptions and report problems and quit the program now.
      catch (HWstack::Overflow)
        {cerr<<"Error: Too many items in the stack"<<Overflow; exit(1);}//handle error
      catch (HWstack::Underflow)
        {cerr<<"Error: Too few items in the stack"<<Underflow; exit(1);}//handle error
      catch (char const* errorcode) // for invalid item
        {cerr<<"Error: Invalid item for stack"<<errorcode; exit(1);}//handle error

      i++; // go to next position in expression
           // go back through the loop
   }// end of while

  //  After the loop successfully completes:
  //  The result will be at the top of the stack. Pop it and show it.
  postfixstack.pop(result);
  cout<< "result: " << result <<endl;

  //  If anything is left on the stack, an incomplete expression was found
  // inform the user.
  if (!postfixstack.isEmpty())
    { cout<<"Incomplete expression!"<<endl;}

}//end of program

ERRORS:

stackclient.cpp:147: error: no matching function for call to âstack::pop(int&)â
HWstack.h:32: note: candidates are: void stack::pop(el_t&)
stackclient.cpp:148: error: no matching function for call to âstack::pop(int&)â
HWstack.h:32: note: candidates are: void stack::pop(el_t&)
stackclient.cpp:179: error: expected type-specifier before âHWstackâ
stackclient.cpp:179: error: expected `)' before â::â token
stackclient.cpp:179: error: expected `{' before â::â token
stackclient.cpp:179: error: â::Overflowâ has not been declared
stackclient.cpp:179: error: expected `;' before â)â token
stackclient.cpp:181: error: expected primary-expression before âcatchâ
stackclient.cpp:181: error: expected `;' before âcatchâ
stackclient.cpp:183: error: expected primary-expression before âcatchâ
stackclient.cpp:183: error: expected `;' before âcatchâ
stackclient.cpp:192: error: no matching function for call to âstack::pop(int&)â
HWstack.h:32: note: candidates are: void stack::pop(el_t&) 
Last edited on
What is in "HWstack.h"? You can't tell us you have a car problem and then only give us the steering wheel.
You haven't provided hwstack.h, so can't comment on any issues in your header file. Note that several of your error messages refer to your header file.

Some other comments:
Line 37: What id the purpose of this if statement? Casting a char to an int will result in the same binary value. i.e. This if statement will always be true.

You're dealing with one character at a time from expression. You're not doing any conversion from numeric characters entered to integers (casting won't do this). What if I enter 123 ?

Line 22: cin only reads up to whitespace. You probably want to use getline to get the entire line. If you do do, you'll want to ignore whitespace as as you're parsing the expession.

edit: The line numbers in your posted code do not agree with the line numbers in your error messages. That makes it difficult to find errors in your code.
Last edited on
Topic archived. No new replies allowed.