Thread: Stack using push and pop

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    48

    Unhappy Stack using push and pop

    Hi everyone, I've been doing pretty well while learning C++. Unfortunaltely I ve been having major problems with Classes since this is very new to me. I've been trying for a few days now to write a program that will allow a user to continuoisly enter numbers (as long as it is less than 9) and have them pile up on each other using "push". I want the program to also pop all of the elements leaving only the high number (above 9). At first I tried to have the user pop the number themselves and then use peek to look at the stacks contents but became so frustrated that I just tried to get one part working successfully. Unfortunately this part is not working either. Can you pelase guide me in the right directions because I feel as if I am way off; I even went out to buy a C++ book by Deitel and Deitel but am still having a little trouble.
    Any help would be greatly appreciated

    Code:
    #include <iostream.h>
    const int Stack_Size=100;
    /*The stack itself*/
    
    class Stack{
    public:
    	stack(void);/*initialize the stack*/
    	void push (const int item); /*push an item*/
    	int pop (void);
    	int peek (const int item);
    	bool empty (const int item); /*is stack empty?*/
    	bool full (const int item); /* Is stack full*/
    
    private:
    	int count; /*number of items in the stack*/
    	int data [Stack_Size]; /*The items themselves*/
    };
    
    	
    #include "Stack.h"	
    
    Stack::Stack()
    		{
    			count=0; /*initialize the top of the stack*/
    	
    		void Stack::push(const int item)
    		{
    			count++;
    			data[count]=item;
    		}
    		int Stack::pop()
    		{
    			int topval;
    
    			topval=data[count];
    			count--;
    			return(topval);
    		}
    
    		int Stack::empty()
    		{
    			if (count==0)
    				return TRUE;
    			else
    				return FALSE;
    		}
    		int Stack::full()
    		{
    			if (count==Stack_Size)
    				return TRUE;
    			else
    				return FALSE;
    		}
    
    	
    	int main()
    		{
    			Stack contents;
    			int number;
    cout<<" Enter as many digits as you'd like per line"
    <<" To stop this program please enter a number greater than 9";
    
    
    while(1)
    {
    	cout<<"enter a digit";
    	cin>>number;
    	if (number>9)
    		break;
    	if (contents.full())
    		cout<<"no more space";
    break;
    
    else
    
    	contents.push(number);
    
    
    	cout<<"The values that were popped from the stack are";
    	while (!contents.empty())
    	{
    		cout<<contents.pop()<<endl;
    	}
    	return 0;
    	}

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    if your stack size is Stack_size = 100, then I would expect to be able to put up to 100 items/elements in the stack. However, data[0] will never be used in your code, so the max number of items/elements that can be entered by the user will be 99, not 100.

    Likewise if you let count grow to 100, then you will (hopefully) crash the program when count == 100 in the following line

    data[count]=item;

    To fix this use count - 1 in all the []'s rather than count

    change the first while loop conditional to this:

    while(number < 10 && count < 101)

    Then when the loop stops figure out why and tell the user. That way you get rid of all the breaks and allow the user to add up to 100 values less than 9. You might want to add some further data verification to make the program more robust, but that can come later.

    on first glance you appear to have a mismatch between number of right and left curly braces. The first while statement doesn't have a closing brace, I think. To many breaks to be sure though.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    well first this:
    Code:
    stack(void);/*initialize the stack*/
    Should be
    Code:
    Stack(void);/*initialize the stack*/           //must have big S
    You will have to rewrite this:
    Code:
    Stack::Stack()
    		{
    			count=0; /*initialize the top of the stack*/
    	
    		void Stack::push(const int item)
    		{
    			count++;
    			data[count]=item;
    		}
    		int Stack::pop()
    		{
    			int topval;
    
    			topval=data[count];
    			count--;
    			return(topval);
    		}
    
    		int Stack::empty()
    		{
    			if (count==0)
    				return TRUE;
    			else
    				return FALSE;
    		}
    		int Stack::full()
    		{
    			if (count==Stack_Size)
    				return TRUE;
    			else
    				return FALSE;
    		}
    Functions shouldnt be declared inside the constructor: watch this code as an example:
    Code:
    class someclass
    {
        someclass();
        void somefunction();
        int somenumber;
    };
    someclass::someclass()
    {
        somenumber = 0;
    }
    
    void someclass::somefunction()
    {
        //some code here
    }
    Code:
    while(1)
    {
    	cout<<"enter a digit";
    	cin>>number;
    	if (number>9)
    		break;
    	if (contents.full())
    		cout<<"no more space";
    break;
    
    else
    
    	contents.push(number);
    
    
    	cout<<"The values that were popped from the stack are";
    	while (!contents.empty())
    	{
    		cout<<contents.pop()<<endl;
    	}
    	return 0;
    	}
    well you should check use of { and }
    you have a little too few here: I *think* this is what you meant:
    Code:
    while(1)
    {
    	cout<<"enter a digit";
    	cin>>number;
    	if (number>9)
    		break;
    	if (contents.full())
                    {
    		cout<<"no more space";
                                    break;
                    }
    
                    else
                       contents.push(number);
    }
    
    	cout<<"The values that were popped from the stack are";
    	while (!contents.empty())
    	{
    		cout<<contents.pop()<<endl;
    	}
    
    	return 0;
    	}
    Or something like that, but you had a little too few brackets. Also please post the errors you get (if you get any but you should). Also like it is in your code now you have declared main as a part of the constructor to the Stack class.
    Last edited by Shakti; 11-03-2003 at 12:48 PM.

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    48

    Stack-Almost there

    Hi, thanks for the help everyone. I rewrote the program (minus the main so that I can work on it) but am receiving an error message regarding my bool statements.

    error C2511: 'empty' : overloaded member function 'bool (bool)' not found in 'Stack'



    Code:
    #include <iostream.h>
    const int Stack_Size=100;  /*The stack itself*/
    
    class Stack{
    public:
    	Stack(void);/*initialize the stack*/
    	void push (const int item); /*push an item*/
    	int pop (void);
    	int peek (const int item);
    	bool empty (const int item); /*is stack empty?*/
    	bool full (const int item); /* Is stack full*/
    
    private:
    	int count; /*number of items in the stack*/
    	int data [Stack_Size]; /*The items themselves*/
    };
    
    
    
    Stack::Stack()
    	{
    			count=-1; /*initialize the top of the stack*/
    	}
    		void Stack::push(const int item)
    		{
    			count++;
    			data[count]=item;
    		}
    		int Stack::pop()
    		{
    			int value;
    
    			value=data[count];
    			count--;
    			return(value);
    		}
    
    
    
    		bool Stack::empty()
    		{
    			if (count==-1)
    				return TRUE;
    			else
    				return FALSE;
    		}
    		bool Stack::full()
    		{
    			if (count==Stack_Size-1)
    				return TRUE;
    			else
    				return FALSE;
    		}

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    looks good. You can either put all of that before main() or put the first part (class Stack in a header file called Stack.h and the rest in a cpp file called Stack.cpp and then list an include like this:
    #include "Stack.h" at the start of the program and not list all the stuff before main(). Either way, time to start redoing the driver program (main()) to test you Stack class.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Disable Smilies in This Post
    Please remember this when posting C++ code
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help calling function is asm
    By brietje698 in forum C++ Programming
    Replies: 24
    Last Post: 12-06-2007, 04:48 PM
  2. Getting position from game..
    By brietje698 in forum C++ Programming
    Replies: 1
    Last Post: 10-26-2007, 12:15 PM
  3. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM