Stack not displaying items

Hi..
I'm doing an implementation of stack
My program is working fine..
but when I displays the stack item.. it doesn't display the last item..
rest my program is fine (according to me )
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
#include <iostream>
using namespace std;
// class of stack

const int Max_Size=9;	// maximum size of stack
class Stack
{
public:
	
	char theStack[Max_Size];
	int top;	// indicates to the top of stack


	Stack();	// Default constructor

	void clear();	// clear the stack
	bool push (char);	// push item into stack
	char pop ();	// return the element
	//int top();	// copy the element of stack

	bool isFull();	// checks if the stack is full
	bool isEmpty();	// check is the stack is empty

	void Display();	// Displays the elements of stack
};

Stack::Stack()
{
	//cout<<"Stack initialized with default value\n";
	top=-1;
}

void Stack::clear()
{
	top =-1;	// Reset the top
}

bool Stack::push(char ch)
{
	//if (isFull())
		//return false;	// return false if the stack is full
	top++;	// increment in the top of stack
	theStack[top]=ch;
	return true;	// return true after pushing the element into the stack

}

char Stack::pop()
{
	char ch;

	if (isEmpty())
		return 0;
	else
	{
		ch= theStack[top];	// return the top element of stack
		top--;	// make decrement in the top index of stack
		return ch;
		
	}
}

bool Stack::isEmpty()
{
	// checks if top of stack is empty
	return (top==-1);
}

bool Stack::isFull()
{
	// check if the stack is full
	return (top>=Max_Size);
}

void Stack::Display()
{
	cout<<"Stack has "<< top+1 <<" elements\n\n";
	
	for (int i=0; i<top; i++)
	{
		cout<<theStack[i]<<endl;
	}
}

int main ()
{
	Stack StackObj;	// pointer of stack type

	cout<<"Stack implementation\n";
	cout<<"Creating stack....\n";
	
	
	cout<<"Stack created\n\n\n";
	cout<<"\Pushing the element 'A' in the stack\n";
	
	StackObj.push('A');
	StackObj.Display();

	cout<<"\nPushing the element 'B' in the stack\n";
	
	StackObj.push('B');
	StackObj.Display();

	cout<<"\nPushing the element 'C' in the stack\n"; 
	
	StackObj.push('C');
	StackObj.Display();

	cout<<"\nPushing the element 'D' in the stack\n";
	
	StackObj.push('D');
	StackObj.Display();

	cout<<"\nPushing the element 'E' in the stack\n";
	
	StackObj.push('E');
	StackObj.Display();

	cout<<"\nAfter poping the element\n";
	
	StackObj.pop();
	StackObj.Display();

	return 0;


	
}
If there are top+1 elements, your loop condition should be i < top+1
@LB Thanks a lot mate.. that worked for me..
But the condition still confuses me .. I' still doesn't understand the condition
If you have n elements, you use this loop to loop through them all:

for(std::size_t i = 0; i < n; ++i)

This is a traditional for loop and is what you should always write first. No weird <= stuff in the condition, just i < n

In your case, you tell the user that the value of n is top+1, so you should believe it yourself too.
@LB I've seen many code examples of you.. in every piece of code you repeat std::anything everywhere.. why you don't once write using namespace std; above main function and not repeat always every where in your code
You don't need the std:: , alot of pro programmers on here like to use it so you don't accidentally override a built-in function/library with one of your custom ones.

Like LB said though.. you tell the user that the stack is size top + 1 so you must iterate there to get the results you are looking for.
Last edited on
using namespace std; is bad. Google it.

http://stackoverflow.com/q/1452721/1959975

There have been specific incidents on this forum where someone was getting a compilation error that could have been avoided by not using namespace std;. There are a lot of things in the std namespace and unless you have memorized them all you can't avoid name collisions.
Last edited on
alot of pro programmers on here like to use it so you don't accidentally override a built-in function/library with one of your custom ones.
what does this mean ??
Any example, I'll highly appreciate..
I posted while you were posting.
I googled it... but still confusions..
The answer I consider is following, I found this on StackOverflow..


This is not related to performance at all. But consider this: You are using two libraries called Foo and Bar:

using namespace foo;
using namespace bar;

Everything works fine, you can call Blah() from Foo and Quux() from Bar without problems. But one day you upgrade to a new version of Foo 2.0, which now offers a function called Quux(). Now you've got a conflict: Both Foo 2.0 and Bar import Quux() into your global namespace. This is going to take some effort to fix, especially if the function parameters happen to match.

If you have used foo::Blah() and bar::Quux() then the introduction of foo::Quux() would have been a non-event.

Do the collision can still occur if we are using only 1 namespace ?
Yes. An example from this very forum is that someone created a class named hash unaware that there was already a std::hash function. When they tried to use their class it failed to compile.
Last edited on
where can I have the list of all std:: all functions ??
Why we pass arguments to main functions main (int argc, char*argv[])

I've read the article on cplusplus http://www.cplusplus.com/articles/DEN36Up4/
but how is this use full... looking for any example.. ??

Thanks in advance
Last edited on
It is a way to easily get command line arguments that were specified when the program was run.

1
2
3
4
5
6
7
8
9
10
11
12
run fooobj -d
[code] 

In this case the -d argument can be accessed through argv[1].

[code] 
int main (int argc, const char *argv[])
{  bool debug_sw = false; 

    if (strcmp(argv[1], "-d") == 0)
       debug_sw = true;
...
Topic archived. No new replies allowed.