every time i run the program it prints out 0. I think something is wrong with my display function but i cant see the problem.

#include <iostream>
       using namespace std;
        #define STACKSIZE 10
        class stack
        {
                private:
                        int arr[STACKSIZE+1];
                        int tos;
                public:
                        stack();
                        void push(int x);
                        int  pop();
                        bool is_empty();
                        bool is_full();
                        int  size();
                        void display();
        };
 
        stack::stack()
        {
                tos = 0;
        }
 
        void stack::push(int x)
        {
                if(!is_full())
                        arr[tos++] = x;
                else
                        cout << "Stack is full, Can not push " << x << endl;
        }
 
        int stack::pop()
        {
                if(!is_empty())
                        return arr[--tos];
                else
                        cout << "Stack is empty, cannot pop" << endl;
                return -1;
        }
 
        bool stack::is_empty()
        {
                if(tos == 0)
                        return true;
                else
                        return false;
        }
 
        bool stack::is_full()
        {
                if(tos == STACKSIZE)
                        return true;
                else
                        return false;
        }
 
        int stack::size()
        {
                return tos;
        }
 
        void stack::display()
        {
                if(tos == 0)
                {
                        cout << "No elements to display" << endl;
                        return;
                }
				else
				{
					for(int i=0;i<STACKSIZE;i++)
					{
						cout << arr[i] << " ";
						cout << endl;
					}
				}
        }
 
        int main()
        {
                stack mystack;
                cout << mystack.size() << endl;
                mystack.push(1);
		mystack.push(2);
		mystack.push(3);
                if(mystack.is_full())
                        cout << "stack is full" << endl;
                mystack.pop();
                if(mystack.is_empty())
                        cout << "stack is empty" << endl;
        }

Recommended Answers

All 2 Replies

You never call your display function. On line 82 you have cout << mystack.size() << endl; , which is where your zero comes from.

the for loop on the display function should probably be for(int i=0;i<tos;i++)

try this.

#include <iostream>
       using namespace std;
        #define STACKSIZE 10
        class stack
        {
                private:
                        int arr[STACKSIZE+1];
                        int tos;
                public:
                        stack();
                        void push(int x);
                        int  pop();
                        bool is_empty();
                        bool is_full();
                        int  size();
                        void display();
        };

        stack::stack()
        {
                tos = 0;
        }

        void stack::push(int x)
        {
                if(!is_full())
                        arr[tos++] = x;
                else
                        cout << "Stack is full, Can not push " << x << endl;
        }

        int stack::pop()
        {
                if(!is_empty())
                        return arr[--tos];
                else
                        cout << "Stack is empty, cannot pop" << endl;
                return -1;
        }

        bool stack::is_empty()
        {
                if(tos == 0)
                        return true;
                else
                        return false;
        }

        bool stack::is_full()
        {
                if(tos == STACKSIZE)
                        return true;
                else
                        return false;
        }

        int stack::size()
        {
                return tos;
        }

        void stack::display()
        {
                if(tos == 0)
                {
                        cout << "No elements to display" << endl;
                        return;
                }
				else
				{
					for(int i=0;i<STACKSIZE;i++)
					{
						cout <<i <<"   =    "<< arr[i] << " ";
						cout << endl;
					}
				}
        }

        int main()
        {
                stack mystack;
                cout << mystack.size() << endl;
               for(int i =0;i<STACKSIZE;i++){
            	   mystack.push(i*STACKSIZE);
               }

		mystack.display();
                if(mystack.is_full())
                        cout << "stack is full" << endl;
                mystack.pop();
                if(mystack.is_empty())
                        cout << "stack is empty" << endl;
        }
/***
OUTput
0
0   =    0 
1   =    10 
2   =    20 
3   =    30 
4   =    40 
5   =    50 
6   =    60 
7   =    70 
8   =    80 
9   =    90 
stack is full
*/
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.