Help with program, converting array stack into vector stack

Hello everyone, so a while back I wrote this program that solves post-fix expressions using stack arrays. the program works fine as it should have, but now I want it to do the same but now using stack vector using the same functions i have there, the main function should not change, I'm just trying to do the necessary tweeks to the header and implementation functions so it now uses a vector. Also notice how I used a constant max size in the array, for the vector there will be NO maximum size meaning that it will grow as elements are added. The isFull function should return false since there's no max size. Could you guys help me, I'm really rusty on my vector syntax?
Here's my functional program using stack array:

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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include <iostream>
#include <string>
#include <cmath>
using namespace std;


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

typedef int el_t;      // the el_t type is ** 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 of stack

   public: // prototypes to be used by the client

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

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

        //  pass the element to be pushed
        // PURPOSE: if not full, enters an element at the top;
        //          otherwise throws an exception - Overflow
        void push(el_t);

        // provide variable to receive the popped element
        // PURPOSE: if not empty, removes and gives back the top element;
        //          otherwise throws an exception - Underflow
        void pop(el_t&);

        // provide variable to receive the top element 
        // PURPOSE: if not empty, gives the top element without removing it;
        //          otherwise, throws an exception - Underflow
        void topElem(el_t&);

        // if array is empty returns true, otherwise it returns false
      	bool isEmpty();

	// if array is full returns true, otherwise it returns false
      	bool isFull();

	// prints all the elements of the array, otherwise throws an exception - Underflow
        void displayAll();

	// removes all the elements from the array, otherwise throws an exception - underflow
        void clearIt();
        
  };  





///////////////////////////////////////////////////////////////////////////////////////////////////////



// The default constructor initializes top to -1
stack::stack()
{ 
  top = -1; // indicates stack is empty
}

// Since this is a static array the destructor doesn't do anything
stack::~stack()
{
} // nothing }
  
// isEmpty checks to see if top is equal to -1, if true function returns true,
// otherwise isEmpty returns false since top would not be equal to -1
bool stack::isEmpty()
{ if (top == -1) return true; else return false; }
    
// isFull checks to see if the array is full by checking to see if top is equal to array size - 1,
// if true the function returns true, otherwise isFule returns false since the array would not be full
bool stack::isFull()
  { if (top == MAX-1 ) return true; else return false; }
  
// push checks to see if the array is full using isFull function, if isFull returns true,
// push throw exception Overflow, otherwise push adds the element passed to the array
void stack::push(el_t elem)
{ if (isFull())
    throw Overflow();
  else { top++; el[top] = elem; }}
  
// pop makes sure the array isn't empty using the isEmpty function, if isEmpty return true,
// push throws exception Underflow, otherwise pop passes back top element and removes it from stack
void stack::pop(el_t& elem)
  { if (isEmpty())
      throw Underflow();
    else { elem = el[top]; top--;}}

// topElem makes sure array isn't empty using the isEmpty function, if isEmpty reutrns true,
// topElem throws exception Underflow, otherwise topElem passes back top element on the stack
void stack::topElem(el_t& elem)
  { if (isEmpty())
      throw Underflow();
    else { elem = el[top]; } }

//  dislayAll calls isEmpty and if true, displays [ empty ].
//  Otherwise, diplays the elements vertically.
void stack::displayAll()
  {  if (isEmpty()) cout << ".[ empty ]." << endl;
    else for (int i=top; i>=0; i--)
      { cout << el[i] << endl; }}

// clearIt makes sure array isn't empty using the isEmpty function, if isEmpty returns true,
// clearIt throws exception Underflow, otherwise clearIt uses the pop function to remove all
// elements from the stack.
 void stack::clearIt()
 {
   if(isEmpty())
     throw Underflow();
   else
     {
       el_t fill;
       while(top != -1) // while loop runs until the stack is empty, calls pop each time to remove element from stack
	 {              // and decrements top
	   pop(fill);
	   top--;
	 }
     }
 }





//////////////////////////////////////////////////////////////////////////////////////////////////////////////////




int main()
{
  int i = 0; //counter for while loop
  int number; //holds number to push onto stack
  int box1; //holds first operand
  int box2; //holds second operand
  int result; //holds result of operation done on box 1 and 2

  stack mystack;

  string input; //holds user input
  cout<<"Enter Postfix expression: "<<endl;
  cin>>input;

  while(input[i]!='\0') //continue until end of string 
    {
      try
	{
	  //if only 1 character was entered throw exception
	  if(input.size()==1)
	    throw "Not Enough Values Entered";

	  //if character is 0 through 9
	  if((input[i]>=48) && (input[i]<=57))
	    {
	      number = (input[i]-48); //convert char to an int
	      mystack.push(number); //push int onto stack
	      i++; 
	    }
	  else if((input[i]=='*') || (input[i]=='+') || (input[i]=='-')) 
	    {
	      //if stack isn't empty pop operand into box1, else throw exception
	      if(mystack.isEmpty()!=true)
		mystack.pop(box1);
	      else
		throw "Too few Operands";

	      //if stack isn't empty pop operand into box2, else throw exception
	      if(mystack.isEmpty()!=true)
		mystack.pop(box2);
	      else
		throw "Too few Operands";

	      //if operator +, add operands and store in result
	      if(input[i] == '+')
		result = box1 + box2;

	      //if operator -, subtract box2 from box1 and store in result
	      else if(input[i] == '-')
		result = box2 - box1;

	      //if operator *, multiply operands and store in result
	      else if(input[i] == '*')
		result = box1 * box2;
	      
	      mystack.push(result);//push result onto the stack
	      i++;
	    }
	  else
	    {
	      throw "Entered an invalid Character";
	    }
	}
      catch(stack::Overflow)
	{
	  cerr<<"ERROR: There were Too Many Characters Entered"<<endl; exit(1);
	}
      catch(stack::Underflow)
	{
	}
      catch(char const* errorString)
	{
	  cerr<<"ERROR: "<<errorString<<endl; exit(1);
	}
    }

  mystack.pop(result); //pop final result 

  //if the stack contains more than one value throw exception
  try
    {
      if(mystack.isEmpty()!=true)
	throw "Found Incomplete Expression";
    }
  catch(char const* errorString2)
    {
      cerr<<"ERROR: "<<errorString2<<endl; exit(1);
    }

  cout<<"answer is: "<<result<<endl; //show final result
  
  return 0;
}
Look at this:

http://www.cplusplus.com/reference/stl/vector/

vector has all the function you need (like push_back, pop_back...). Appart from modifing you can use it like an array.

you can omit top and use el.size() instead. el is defined like so: std::vector<el_t> el;
Thank you, I managed to figured it out, it was simpler that I originally thought
Topic archived. No new replies allowed.