Array based stack

I am having trouble getting the error messages to print.
The stack/array doesn't seem to be printing "full" at the max size of 10,

and the error "empty" if the stack is empty.

Does anyone have any suggestions?
Any help is appreciated, thanks!


/*Create a class/struct.

Members:
MaxSize const = 10
Define an array that holds 10 items.
Count - indicates how many items are on the stack.

Methods:
Push
- Accepts a number and adds to the top of the stack.
- If the stack is full emit an error indicating full.
Pop
- Returns a number from the top of the stack.
- If the stack is empty, emit an error indicating the stack is empty.
IsEmpty
- Returns a boolean indicating if the stack is empty.*/


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
#include<iomanip>
#include<iostream>
#include <string>
using namespace std;
 
struct stack 
{

	 private:
			 enum 
			 { 
				 MAX_SIZE = 10
			 };
			
			 int array[MAX_SIZE] ;
			// int count; //= 0;



public:

	int stk[10];											
	int top;
	int count;// = 0;

//public:


	bool full() const 
	{ 
		return count == MAX_SIZE ; 
	}


	/* bool empty() const 
	 { 
		return count == 0 ;
	 }*/


	stack()
	{
		top = -1;
	}

	bool push(int element)
	{
		cout << "\nEnter the element: ";
		cin >> element;



		  if( full() )
		  {
				cerr << "stack is full\n" ;							//Is full not printing
				return false ;
		  }



		stk[++top] = element;

		cout << "Successfully inserted: " << element << "\n";
		
		count++;

		//cout << "There are " << count << " items on the stack\n";					//???
		

	}

		bool empty()
		{
			if(count == 0 ) 
			{
				cout << "\nThe stack is empty\n\n";											//Is empty not printing
				return count == 0;
			}
			else 
			{
				return false;
			}


			for(int i = top; i >= 0; i--)
			{
				cout << stk[i]<< " ";
			}
	}



	int pop()
	{
		if( empty())
		{
			cerr << "\nThe stack is empty\n";											//Is empty not printing
			return -1;
		}
		else
		{
			cout << "\nThe number on the top of the stack is: " << stk[top--] << "\n";	//deleted element?
		}

		 //--count ; // decrement count
			//return array[count] ;
	}





};

int main()
{

	int count = 0;
    stack stk ;
	int ch;

    // push values into the stack
    int v = 1 ;
    
	while( !stk.full() ) 
	{ 
		stk.push(v) ; 
		v *= 2 ; 
	}

    // pop and print them out one by one
    while( !stk.empty() ) 
	{
		cout << stk.pop() << ' ' ;
		cout << '\n' ;
	}


	system("pause");
	return 0;
}
You have forgot to initialize the stack member variable count in the constructor.
When I would initialize count, I would get an error about initializing in the struct.
Do it in the constructor then.
Like this?


1
2
3
4
5
6
7

stack(int count)
	{
               count = 0;
		top = -1;
	}
Yes.
Line 15: it looks like array is unused.
Line 22: should be int stk[MAX_SIZE]; ?
Line 24: get rid of count altogether. The count can be computed as top+1. It's usually a good idea to store data exactly once. That way you don't have to worry about updating multiple copies of the same fundamental thing.
Line 68: add return true;
Lines 85-88 are never executed because the preceding if/else always returns.
Line 102: You need to return the value, not just print it out.
Topic archived. No new replies allowed.