Stack Error Please Help Me..

Hii..
I was given an assignment to implement an Array based template Stack class...
and after 4 hours of figuring out the problem.. I was about to die..
Because this was like a magical error for me
There is nothing written in my main function.. Only an object of stack class is being created.. but it automatically calls 10 times constructor of student class
Here is my code.

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
  //============================================================================
// Name        : DataStructure.cpp
// Author      : Ahsan Ali
// Version     : 1.0
// Copyright   : All Rights Reserved.. Ahsan Corp.
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;
#define MAX_ITEMS 10

class Student
{
private:
	int Reg_ID;
public:
	Student(int Num)
	{
		Reg_ID=Num;
	}

	Student()
	{
		cout<<"Default constructor called automatically\n";
	}

	void SetRollNo(int no)
	{
		Reg_ID= no;
	}
	int GetRollNo()
	{
		return Reg_ID;
	}
	void Display()
	{
		cout<<"Roll No.: "<<Reg_ID<<endl;
	}
};



template <class itemType>
class Stack
{
private:
	int top;
	itemType data[MAX_ITEMS];
public:
	Stack()	// Default constructor
	{
		top=-1;
	}
	void push(itemType item);
	itemType pop();
	bool isEmpty();
	bool isFull();
	void MakeEmpty();
	void Display();
};

template <class itemType>
void Stack <itemType>:: push (itemType item)
{
	if (isFull())
	{
		cout<<"Stack is full, can not push more items\n";
		return;
	}
	top++;
	data[top]= item;
}

template <class itemType>
itemType Stack <itemType>:: pop()
{
	if(isEmpty())
	{
		cout<<"Stack is empty, can not pop more items\n";
		//return 0;
	}
	else
	{
		top--;
		return data[top];
	}
}

template <class itemType>
bool Stack<itemType>:: isEmpty()
{
	return (top==-1);
}

template <class itemType>
bool Stack<itemType>:: isFull()
{
	return (top==MAX_ITEMS-1);
}

template <class itemType>
void Stack<itemType>:: Display()
{
	int top_flag=top;
	Student Temp_Student= data[top--];
	for (int k=0; k<=top_flag; k++)
	{
		Temp_Student.Display();
		Temp_Student= data[top];
		top--;
	}
	top=top_flag;
}

int main()
{
	
	Stack <Student> S1;
	
	cout<<"Calling stack display function\n";
	//S1.Display();

	return 0;
}
but it automatically calls 10 times constructor of student class

Yes it will call the no argument constructor 10 times because it is constructing the 10 elements of the array. If your stack had a size of 100 it would call the constructor 100 times.

Thanks @jlb
More I want to push Student type of objects in my stack.. How can I do that ??
I've tried.. in the same program.. but my program crashed.. Help Needed
Post the code showing what you've tried.

You should also run the program with your debugger, the debugger will be able to tell you exactly where it detects the problem and allow you to view the variables at the time of the crash.

Nothing else, just exactly the same code I'm trying with
Something must be different if your program is now crashing. How are you trying to "push" something onto your stack?

This is my complete program
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
//============================================================================
// Name        : DataStructure.cpp
// Author      : Ahsan Ali
// Version     : 1.0
// Copyright   : All Rights Reserved.. Ahsan Corp.
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <string>
using namespace std;
#define MAX_ITEMS 10

class Student
{
private:
	int Reg_ID;
	int Cell_No;
	string Name;
public:
	Student(int ID, int No, string N)
	{
		Reg_ID=ID;
		Cell_No=No;
		Name=N;
	}

	Student()
	{
		cout<<"Default constructor called automatically\n";
	}

	void SetRollNo(int no)
	{
		Reg_ID= no;
	}
	int GetRollNo()
	{
		return Reg_ID;
	}
	void Display()
	{
		cout<<"Name: "<<Name<<endl;
		cout<<"Registration ID: "<<Reg_ID<<endl;
		cout<<"Contact # "<<Cell_No<<endl;
	}
	void operator=(Student stdnt)
	{
		Name=stdnt.Name;
		Reg_ID=stdnt.Reg_ID;
		Cell_No=stdnt.Cell_No;
	}
};



template <class itemType>
class Stack
{
private:
	int top;
	itemType data[MAX_ITEMS];
public:
	Stack()	// Default constructor
	{
		top=-1;
	}
	void push(itemType item);
	itemType pop();
	bool isEmpty();
	bool isFull();
	void MakeEmpty();
	void Display();
};

template <class itemType>
void Stack <itemType>:: push (itemType item)
{
	if (isFull())
	{
		cout<<"Stack is full, can not push more items\n";
		return;
	}
	top++;
	data[top]= item;
}

template <class itemType>
itemType Stack <itemType>:: pop()
{
	if(isEmpty())
	{
		cout<<"Stack is empty, can not pop more items\n";
		//return 0;
	}
	else
	{
		top--;
		return data[top];
	}
}

template <class itemType>
bool Stack<itemType>:: isEmpty()
{
	return (top==-1);
}

template <class itemType>
bool Stack<itemType>:: isFull()
{
	return (top==MAX_ITEMS-1);
}

template <class itemType>
void Stack<itemType>:: Display()
{
	int top_flag=top;
	Student Temp_Student= data[top--];
	for (int k=0; k<=top_flag; k++)
	{
		Temp_Student.Display();
		Temp_Student= data[top];
		top--;
	}
	top=top_flag;
}

int main()
{
	Student Stud1(130231,431,"Tariq"), Stud2(130332,432,"Saleem"), Stud3(130433,433,"Mateen"), Stud4(130534,434,"Haris");
	Student Stud5(130635,435,"Khizar"), Stud6(130736,436,"Ahmad"), Stud7(130837,437,"Ahsan"), Stud8(130938,438,"Shoaib");
	
	Stack <Student> S1;
	S1.pop();
	S1.push(Stud1);
	S1.push(Stud2);
	S1.push(Stud3);
	S1.push(Stud4);
	S1.push(Stud5);
	S1.push(Stud6);
	S1.push(Stud7);
	S1.push(Stud8);
	
	cout<<"Calling stack display function\n";
	S1.Display();

	return 0;
}
The first thing that is wrong is in your pop() function, you need to return something if it is empty, or make the function a void function and never return anything instead have a function that returns the "top" that would be used before you pop.

The next problem will be in your display function. I suggest you start by making this a const function so you are sure you never change any of the variables of the class. Your display function is changing things it shouldn't be allowed to change. For example it changes top. This variable should only be changed when adding or removing items from the stack.
Thank You very Much jlb..
I know I have problems in these functions.. although not efficient.. but they output correctly..
The main problem I'm having is that I'm unable to push Student class object in stack..
How can I do that..
Thanks in Advance
What makes you think you can push a student object onto the stack?

I know I have problems in these functions.. although not efficient.. but they output correctly..

Really? The pop() function crashes the program when the stack is empty() and the display() function corrupts the state of the class, so to me they don't seem to work properly.

Edit: For the dispaly() class I should have said "possibly corrupts". /Edit

Have you run this program with your debugger?

Last edited on
Thank You @jlb
I know I've problems in my these functions.. Not efficient, but they output correctly..
but the main problem I'm unable to push Student type object into the stack
How can I do that..
ThaNks in Advance.. :)
Here is my complete code
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
//============================================================================
// Name        : DataStructure.cpp
// Author      : Ahsan Ali
// Version     : 1.0
// Copyright   : All Rights Reserved.. Ahsan Corp.
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <string>
using namespace std;
#define MAX_ITEMS 10

class Student
{
private:
	int Reg_ID;
	int Cell_No;
	string Name;
public:
	Student(int ID, int No, string N)
	{
		Reg_ID=ID;
		Cell_No=No;
		Name=N;
	}

	Student()
	{
		//cout<<"Default constructor called automatically\n";
	}

	void SetRollNo(int no)
	{
		Reg_ID= no;
	}
	int GetRollNo()
	{
		return Reg_ID;
	}
	void Display()
	{
		cout<<"Name: "<<Name<<endl;
		cout<<"Registration ID: "<<Reg_ID<<endl;
		cout<<"Contact # "<<Cell_No<<endl;
	}
	void operator=(Student stdnt)
	{
		Name=stdnt.Name;
		Reg_ID=stdnt.Reg_ID;
		Cell_No=stdnt.Cell_No;
	}
};



template <class itemType>
class Stack
{
private:
	int top;
	itemType data[MAX_ITEMS];
public:
	Stack()	// Default constructor
	{
		top=-1;
	}
	void push(itemType item);
	itemType pop();
	bool isEmpty();
	bool isFull();
	void MakeEmpty();
	void Display();
};

template <class itemType>
void Stack <itemType>:: push (itemType item)
{
	if (isFull())
	{
		cout<<"Stack is full, can not push more items\n";
		return;
	}
	top++;
	data[top]= item;
}

template <class itemType>
itemType Stack <itemType>:: pop()
{
	if(isEmpty())
	{
		cout<<"Stack is empty, can not pop more items\n";
		//return 0;
	}
	else
	{
		top--;
		return data[top];
	}
}

template <class itemType>
bool Stack<itemType>:: isEmpty()
{
	return (top==-1);
}

template <class itemType>
bool Stack<itemType>:: isFull()
{
	return (top==MAX_ITEMS-1);
}

template <class itemType>
void Stack<itemType>:: Display()
{
	int top_flag=top;
	Student Temp_Student= data[top--];
	for (int k=0; k<=top_flag; k++)
	{
		Temp_Student.Display();
		Temp_Student= data[top];
		top--;
	}
	top=top_flag;
}

int main()
{
	Student Stud1(130231,431,"Tariq"), Stud2(130332,432,"Saleem"), Stud3(130433,433,"Mateen"), Stud4(130534,434,"Haris");
	Student Stud5(130635,435,"Khizar"), Stud6(130736,436,"Ahmad"), Stud7(130837,437,"Ahsan"), Stud8(130938,438,"Shoaib");
	
	Stack <Student> S1;
	S1.pop();
	S1.push(Stud1);
	S1.push(Stud2);
	S1.push(Stud3);
	S1.push(Stud4);
	S1.push(Stud5);
	S1.push(Stud6);
	S1.push(Stud7);
	S1.push(Stud8);
	
	cout<<"Calling stack display function\n";
	S1.Display();

	return 0;
}
I know I've problems in my these functions.. Not efficient, but they output correctly..
but the main problem I'm unable to push Student type object into the stack
How can I do that..

They don operate correctly!

Fix the problems with your pop() function, or remove the pop() call from main() and you should see that you are able to push the items onto the stack. But you will still need to fix your display() functions so that they don't modify the state of the class. Both of your display() functions should be const qualified.

1
2
3
4
5
6
	void Display() const
	{
		cout<<"Name: "<<Name<<endl;
		cout<<"Registration ID: "<<Reg_ID<<endl;
		cout<<"Contact # "<<Cell_No<<endl;
	}


This will cause a compile error if you try to modify any of the class variables within this read only function.

Last edited on
Very Much Thanks to you Buddy... @ jlb
My program worked correctly as I removed pop(); call from the main..
but how can I fix my pop(); function
Somehow I fixed my program a bit..
although it is working now but it crashes when I run it.
Here is my code
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
//============================================================================
// Name        : DataStructure.cpp
// Author      : Ahsan Ali
// Version     : 1.0
// Copyright   : All Rights Reserved.. Ahsan Corp.
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <string>
using namespace std;
#define MAX_ITEMS 10

class Student
{
private:
	int Reg_ID;
	int Cell_No;
	string Name;
public:
	Student(int ID, int No, string N)
	{
		Reg_ID=ID;
		Cell_No=No;
		Name=N;
	}

	Student()
	{
		//cout<<"Default constructor called automatically\n";
	}

	void SetRollNo(int no)
	{
		Reg_ID= no;
	}
	int GetRollNo()
	{
		return Reg_ID;
	}
	void Display() const
	{
		cout<<"Name: "<<Name<<endl;
		cout<<"Registration ID: "<<Reg_ID<<endl;
		cout<<"Contact # "<<Cell_No<<endl;
	}
	void operator=(Student stdnt)
	{
		Name=stdnt.Name;
		Reg_ID=stdnt.Reg_ID;
		Cell_No=stdnt.Cell_No;
	}
};



template <class itemType>
class Stack
{
private:
	int top;
	itemType data[MAX_ITEMS];
public:
	Stack()	// Default constructor
	{
		top=-1;
	}
	void push(itemType item);
	itemType pop();
	bool isEmpty();
	bool isFull();
	void MakeEmpty();
	void Display() const;
};

template <class itemType>
void Stack <itemType>:: push (itemType item)
{
	if (isFull())
	{
		cout<<"Stack is full, can not push more items\n";
		return;
	}
	top++;
	data[top]= item;
}

template <class itemType>
itemType Stack <itemType>:: pop()
{
	if(isEmpty())
	{
		cout<<"Stack is empty, can not pop more items\n";
		return 0;
	}
	else
	{
		top--;
		return data[top];
	}
}

template <class itemType>
bool Stack<itemType>:: isEmpty()
{
	return (top==-1);
}

template <class itemType>
bool Stack<itemType>:: isFull()
{
	return (top==MAX_ITEMS-1);
}

template <class itemType>
void Stack<itemType>:: Display() const
{
	int top_flag=top;
	Student Temp_Student= data[top_flag--];
	for (int k=0; k<=top; k++)
	{
		Temp_Student.Display();
		Temp_Student= data[top_flag--];
		//top_flag--;
	}
	//top=top_flag;
}

int main()
{
	Student Stud1(130231,431,"Tariq"), Stud2(130332,432,"Saleem"), Stud3(130433,433,"Mateen"), Stud4(130534,434,"Haris");
	Student Stud5(130635,435,"Khizar"), Stud6(130736,436,"Ahmad"), Stud7(130837,437,"Ahsan"), Stud8(130938,438,"Shoaib");
	
	Stack <Student> S1;
	//S1.pop();
	S1.push(Stud1);
	S1.push(Stud2);
	S1.push(Stud3);
	S1.push(Stud4);
	S1.push(Stud5);
	S1.push(Stud6);
	S1.push(Stud7);
	S1.push(Stud8);
	
	cout<<"Calling stack display function\n";
	S1.Display();

	return 0;
}
although it is working now but it crashes when I run it.

Have you run the program with your debugger? Your debugger should be able to show you exactly where the program is crashing and you should be able to view the variables at the time of the crash. Pay attention to the array index variable when you study the variables at the time of the crash. By the way if the program crashes it is not working.

My program worked correctly as I removed pop(); call from the main..
but how can I fix my pop(); function

I've already gave you some hints as to what to do to fix this problem.
The first thing that is wrong is in your pop() function, you need to return something if it is empty, or make the function a void function and never return anything instead have a function that returns the "top" that would be used before you pop.

I see you've tried returning something when the list is empty. But are you returning the correct type of variable (a itemType)? I really recommend the second option I suggested.

Thank You @jlb..
Thank you very much.. I could not have done it with you..
All credit to you.. Have a blessed life..
Thanks Again Buddy...
Topic archived. No new replies allowed.