issue with putting char type into stack

Hi, my program seems to run fine, except for when it I attempt to insert random char types into the stack, where nothing happens and I am unsure why this is, any help is appreciated, thanks!

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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include <iostream>
#include <algorithm>
#include <cassert>
#include <cstdlib>	
#include <ctime>
#include <stack>
#include <typeinfo>
#include <string>
using namespace std;

template <class T>
class container 
{
	template <class T1>
	friend ostream& operator<<(ostream& out, container<T1> &);
	// Postcondition: displays # of values stored in the container, storage 
	//                capacity of the contianer, and stored values in the container .
	//     

public:
	container();
	// Postcondition: set dynamic storage capacity to 1 and count to -1 where (count + 1)
	
   
	void insert(T);
	// Postcondition: if the container is not full, the value passed to the function 
	//                is stored in the first available element of the dynamic array. 
	//                Otherwise the function calls the private "allocate" member function
	//                requesting a new set of dynamic memory with twice the previous storage capacity 
	//			the insert function then increments count by 1 and insert the value into the new and larger array.
	
	void remove();
	//  Precondition: the data array must not be empty; i.e., count must be greater than or equal to 0.
	// Postcondition: if the container is not empty, then remove the most recently stored value ifrom the container and 
	//			decrement count by 1; otherwise, display the message "The container is empty; no action is taken!"
	
	T operator[](T sub);
	//  Precondition: value passed to the function must be a positive integer including 0
	// Postcondition: the value of stored in data[sub] is returned; if sub is out of range, 
	//                display a message and terminate the program .

	bool isFull();
	// Postcondition: return true if the container is full; return false otherwise 

	bool isEmpty();
	// Postcondition: return true if the container is empty; return false otherwise

	T Capacity();
	// Notice uppercase 'C' to avoid conflict with data member named "capacity"
	// Postcondition: returns the current storage capacity of the container
	
	T size();
	// Postcondition: returns the # of elements (# of objects) currently stored in the container

	void resize(T n);
	// Postcondition: container  is resized to n; contents of existing container have been copied to the new array; 
	// 			      old array is deleted to avoid memory leak.

protected:
	void allocate();
	// Postcondition: 1) the capacity of the container has been doubled, 2) existing values in the existing array have been copied to 
	//				   the new and larger dynamic array, 3) memory of the old array has been deleted (returned to "heap").
	
	const static int CAPACITY = 10;
	T data[CAPACITY];	
	int capacity;		// indicates the storage capcity of the container, i.e., the size of the dynamic array		
   	int count;		// used as a subscript to index into the array; size = count + 1
};

template <class T>
container<T>::container()
{
    capacity = 1;
	count = -1;	// container is empty
}

template <class T>
void container<T>::insert(T value)
{
	if (!isFull())
	{	
        data[++count] = value;
	}
	
	else 
    {
		count = count + 1;
	}
}

template <class T>
void container<T>::remove()
{
    if(!isEmpty())
    {
    	for (int i = 0; i < capacity; i++)
    	{
    		--count;
		}
	}
	
	else
	cout << "Container is empty!" << endl;
}

template <class T>
T container<T>::operator[](T sub)
{
    if(sub <= capacity)
	{
		return data[sub];
	}
	else
	{
		cout << endl << "Sub is out of range; program is terminated!";
		exit(1);
	}

}

template <class T>
bool container<T>::isFull()
{ 
    if (count != capacity)
    return false;
    
    else
    return true;

}

template <class T>
bool container<T>::isEmpty()
{ 
	if (count == -1)
	return true;
	
	else
	return false;
}

template <class T>
T container<T>::Capacity()
{
    return capacity;
}

template <class T>
T container<T>::size()
{
   return count + 1;
}

template <class T>
void container<T>::resize(T n)
{
	int *temptr;
	capacity = n;
	temptr = new int[n + 1];
	
	if(n > count + 1)
	{
	    for(int i = 0; i < count + 1; i++)
    		temptr[i] = data[i];
    	    for(int i = count + 1; i < n; i++)
    		temptr[i] = int();
	}
	
	else
	{
	    for(int i = 0; i < n; i++)
    		temptr[i] = data[i];
	}
	
}

template <class T1>
ostream& operator<<(ostream& out, container<T1> &c)
{
    out << "Container capacity = " << c.Capacity();
    out << " Size or actual # of values stored in the container = " << c.size() << endl;
    out << "Contents of the container = ";
    
    if(c.isEmpty() == true)
	{
		out << "empty!";
	}
	
	else
	{
		for(int i = 0; i < c.count + 1; i++)
			out << c.data[i] << "  ";
	}

	return out;
    
}

template <class T>
void container<T>::allocate()
{
	capacity = capacity * 2;
	
	resize(capacity);
	
}


template <class T>
class Stack : protected container<int>
{
	public:
		Stack();
		void push(T);
		T top();
		void pop();
		bool empty();
		bool full();
		void erase();
	
};
template <class T>
Stack<T>::Stack() : container<int>()
{
	
}

template <class T>
void Stack<T>::push(T n)
{
	insert(n);
}

template <class T>
T Stack<T>::top()
{
	if(!isEmpty())
	return data[count];
	
	else
	{
		cout << "Stack is empty!" << endl;
		return -99;
	}
}

template <class T>
void Stack<T>::pop()
{
	if(!isEmpty())
	remove();
	
	else
	{
		cout << "Stack is empty! Program terminated!" << endl;
		system("PAUSE");
		exit(1);
	}
}

template <class T>
bool Stack<T>::empty()
{
	return isEmpty();
}

template <class T>
bool Stack<T>::full()
{
	return isFull();
}

template <class T>
void Stack<T>::erase()
{
	while(!empty())
	{
		cout << top() << " has been removed from the stack!" << endl;
		pop();
	}
}

char genRandom()
{
	static const char alphanum[] =
	"0123456789"
	"!@#$%^&*"
	"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	"abcdefghijklmnopqrstuvwxyz";
	
	int stringLength = sizeof(alphanum) - 1;
	
	return alphanum[rand() % stringLength];
}

template <class T>
void writeStack(T& stk)
{
	if (stk.empty())
		cout << "stack is empty!";
	else
	{
		cout << "stack is not empty!" << endl;
		cout << endl;
		cout << "We now pop 5 times";
		cout << endl << endl;
	
		for(int i = 0; i < 5; i++)
		{
			cout << stk.top() << " has been pushed off of the stack" << endl;
			cout << endl;
			stk.pop();
		}
	}
}

int main()
{
	Stack<int> stk;

	if (stk.empty())
		cout << "stack is empty!";
	else
		cout << "stack is not empty!" << endl;

	stk.push(11);
	cout << endl << stk.top() << " has been pushed on top " << endl;

	if (stk.empty())
		cout << "stack is empty!";
	else
		cout << "stack is not empty!" << endl;


	cout << "We now perform the pop operation:" << endl;
	cout << stk.top();
	stk.pop(); 
	cout << endl;
	if (stk.empty())
		cout << "stack is empty!";
	else
		cout << "stack is not empty!" << endl;

	srand(static_cast<unsigned int> (time(0)));
	cout << "we now push 5 random int numbers onto empty stack " << endl;
	
	for(int i = 0; i < 5; i++)
	{
		stk.push(rand() % 99 +1);
		cout << stk.top() << " has been pushed onto the stack," << endl;
	}
	
	writeStack(stk);
	
	Stack<char> stk2;
	
	srand(static_cast<unsigned char> (time(0)));
	cout << "we now push 10 random char letters onto empty stack " << endl;
	for(int i = 0; i < 10; i++)
	{
		char c;
		c = (rand() % 26 + 65);
		stk.push(c);
	}
	
	writeStack(stk2);
	
	
    system("PAUSE");
    return 0;
}
1
2
3
4
5
6
7
8
9
10
  Stack<char> stk2;
	
  srand(static_cast<unsigned char> (time(0)));
  cout << "we now push 10 random char letters onto empty stack " << endl;
  for(int i = 0; i < 10; i++)
  {
    char c;
    c = (rand() % 26 + 65);
    stk.push(c);
  }


Shouldn't you push the chars onto stk2 ?
These type of errors are easier to spot if you give your variables a little more meaningful names - like CharStack and IntStack
Topic archived. No new replies allowed.