Thread: stack array

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    41

    Arrow stack array

    hi the requirement is i have to use two stacks for input and one for output
    apply some binary operation on two stacks and store the result in 3rd stack
    here is my code
    Code:
    # include <iostream>
    # include <conio.h>
    # include <stack>
    using namespace std;
    class intstack
    {
    private:
    	int * stackarray;
    	int stacksize;	
    	int top;
    public:
    	intstack (int);
    	void push ();
    	void pop ();
    	bool isfull (void);
    	bool isempty (void);
    	intstack add (intstack & o);
    };//end class
    intstack::intstack (int size)
    {
    	stackarray=new int[size];
    	stacksize=size;
    	top=-1;
    
    }//end definition
    void intstack::push ()
    {
    	if(isfull())
    	{
    		cout<<"stack is full"<<endl;
    	
    	}//end if
    	else 
    	{
    		top++;
    		for (int i=0;i<stacksize-1;i++)
    		{
    			cin>>stackarray[top];
    		
    		}
    	
    	}
    
    }//end push
    void intstack::pop ()
    {
    	int num;
    	if(isempty())
    	{
    		cout<<"stack is empty"<<endl;
    	
    	}
    	else 
    	{
    		num=stackarray[top];
    		top--;
    		cout<<num;
    	
    	}
    }//end 
    bool intstack::isfull (void)
    {
    	bool status;
    	if (top==stacksize-1)
    	{
    		status=true;
    	
    	}
    	else 
    	{
    		status=false;
    	
    	}
    	return status;
    
    }
    bool intstack::isempty (void)
    {
    	bool status;
    	if (top==-1)
    	{
    		status=true;
    	
    	}
    	else 
    	{
    		status=false;
    	
    	}
    	return status;
    
    }
    intstack intstack::add (intstack & o)
    {
    	intstack temp(stacksize);
    
    	temp.top=top;
    	
    	for (int i=0; i<stacksize-1; i++)
    	{
    		temp.stackarray[i]=stackarray[i]+o.stackarray[i];
    		
    	
    	}
    	return (temp);
    
    
    }
    int main ()
    {
    	int var=0;
    	intstack stack1(5),stack2(5),stack3(5);
    	cout<<"enter numbers in first stack"<<endl;
    	stack1.push ();
    	cout<<"enter number in second stack"<<endl;
    	stack2.push ();
    	stack3=stack2.add (stack1);
    	stack3.pop ();
    	stack3.pop ();
    	stack3.pop ();
    	stack3.pop ();
    	getch ();
    }//
    the problem is that when i execute this code it runs but it shows the result of last array element but not others and say "stack is empty".
    for example i have entered in first array
    1
    2
    3
    4
    numbers in 2nd array
    5
    6
    7
    8
    then it shows
    12
    stack is empty
    stack is empty
    stack is empty
    can anyone correct my code please

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    void intstack::push ()
    {
    	if(isfull())
    	{
    		cout<<"stack is full"<<endl;
    	
    	}//end if
    	else 
    	{
    		top++;
    		for (int i=0;i<stacksize-1;i++)
    		{
    			cin>>stackarray[top];
    		
    		}
    	
    	}
    
    }//end push
    If you write to the same memory location four times, what are the odds you will actually get to keep all four numbers?

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    41
    i got it thanks tabstop
    now its is working

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 02:56 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  5. stack implementation problem-help needed
    By sanju in forum C Programming
    Replies: 1
    Last Post: 12-10-2002, 07:29 AM