Stack problem

This is a discussion on Stack problem within the C++ Programming forums, part of the General Programming Boards category; Hi everyone, I had a question regarding my current program. I have just about completed it but have noticed a ...

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

    Stack problem

    Hi everyone, I had a question regarding my current program. I have just about completed it but have noticed a small error. In my peek function, I want the program to look through the entire stack for the number 5, but I've noticed that it only looks to the very last number (not counting a number greater then 9).
    For instance if I enter (1,2,3,4,5) it will return stating that the number 5 is in the stack
    but
    if I enter (5,4,3,2,1) it will return stating that the number 5 is not in the stack when it obviously is. I've looked over the program a few times and cannot see where I went wrong because every change I've tried to make so far has produced an error.
    Please help

    Any help would greatly be appreciated

    Thanks

    Code:
    #include <iostream.h>  //This is my preprocessor directive for the cout/cin statements//
    const int Stack_Size=10; //Initialization of Stack Size//
    
    
    class Stack
    {
    
    public:
    	Stack(); //constructor//
    	void push(int);
    	int pop();
    	bool peek();
    	bool empty();
    	bool full();
    
    	private:
    	int count; 
    	int data[Stack_Size]; 
    
    };
    
    		Stack::Stack()  
    		{
    			count=-1;
    		}
    		void Stack::push(int value)
    		{
    			count++;
    			data[count]=value;
    		}
    		int Stack::pop()
    		{
    			int topvalue;
    
    			topvalue=data[count];
    			count--;
    			return(topvalue);
    		}
    
    		bool Stack::peek()
    		{
    		if (data[count]==5) //If 5 is within the stack this function will return 1 (true)
    		return 1;
    		else                //else it will return 0 (false)
    			return 0;
    	
    		}
    		bool Stack::empty()
    		{
    			if (count==-1) //If the stack is empty this function will return 1 (true)
    				return 1;
    			else
    				return 0;
    		}
    		bool Stack::full()
    		{
    			if (count==Stack_Size-1)
    				return 1;
    			else
    				return 0;
    		}
    
    	
    	int main()
    		{
    			Stack entries;
    			int number;
    			char answer;
    			
    		
    cout<<" Please enter as many digits as you wish.\n  If you wish to end your entries, please enter a number greater than 10\n  ";
    
    		
    while(1)
    {
    	cout<<"Digit:\n";
    	cin>>number;
    	if (number>9) //If the entry is >9, the loop will break and that number will not count in the stack//
    		break;
    	if (entries.full())
                    {
    		cout<<"The stack is full and you do not have any space left\n";
                                    break;
                    }
    
                    else
                       entries.push(number);
    					
    }
    		cout<<"Is value 5 in the stack (peek): ?\n";
    			
    			if (entries.peek())
    			{
    				cout<<"This stack does contain value 5\n";
    			}
    				else
    					cout<<" This stack does not contain value 5\n";
    
    	
    	if (entries.empty())
    	{
    		cout<<"The stack is empty\n";
    		
    	}
    	else
    	
    	while (!entries.empty())
    	{
    		
    			cout<<" Do you want to pop an item from the stack?  ";
    			cin>>answer;
    			if (answer=='y')
    			cout<<entries.pop()<<endl;
    			else
    				break;
    	}
    
    
    	return 0;
    	}

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Your peek method doesn't search the entire stack. It only looks to see if the top entry on the stack is a 5.

    Also (these have nothing to do with that error):
    Your push method should check to see if the stack is full before it does anything else (and return with an error if it is full).
    Your pop method should check to see if the stack is empty before doing anything else (and return with an error if it is empty).

  3. #3
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    Originally posted by R.Stiltskin
    Your peek method doesn't search the entire stack. It only looks to see if the top entry on the stack is a 5.

    Also (these have nothing to do with that error):
    Your push method should check to see if the stack is full before it does anything else (and return with an error if it is full).
    Your pop method should check to see if the stack is empty before doing anything else (and return with an error if it is empty).
    or instead of an error message (or along with) you can throw an empty or full class stack. In your peek function you just search the array at count....so if the value that you are looking for accidentely happens to be the same value as the count element than it will tell you that you have found the number.

    I would make a recursive function, or maybe a for loop....maybe somehting like this:
    Code:
    //check if stack empty
    for ( i = 0; i <=count; i++ ) {
          if ( data[i] == value_youre_looking for ) 
                return true;
    }

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  4. #4
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571

    Re: Stack problem

    Originally posted by silicon
    Hi everyone, I had a question regarding my current program. I have just about completed it but have noticed a small error. In my peek function, I want the program to look through the entire stack for the number 5, but I've noticed that it only looks to the very last number (not counting a number greater then 9).
    Why are you using a stack if you care about more than the top member? Use something like a linked list, which is pretty much the same thing, only it won't confuse everyone when you iterate through it.

    In general, you don't iterate through stacks, but put things on and take them off, ONLY looking at the top member, not the ones underneath.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Problem?
    By audinue in forum C++ Programming
    Replies: 3
    Last Post: 05-16-2009, 12:28 PM
  2. Stack problem - I've hit a wall!
    By miniwhip in forum C Programming
    Replies: 7
    Last Post: 11-14-2007, 03:05 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Having problem deleting node in middle of stack
    By sballew in forum C Programming
    Replies: 3
    Last Post: 10-29-2001, 11:00 AM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 06:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21