Thread: help me about output queue

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    20

    help me about output queue

    Code:
    int MyQueue::get() 
    {
    	if ( empty() == false ) //queue not empty
    	{
    		top--;
    		return IArr[top1++]; //input result queue
    	}
    	else
    		cout << " Queue empty "; //queue rong
    }
    main()
    Code:
    MyQueue Q;
        Q.put(1);	//Queue contains 1
        Q.put(2);	//Queue contains 1 2
        cout << Q.get(); //Prints 1, queue contains 2 3
        cout << Q.get(); //Prints 2, queue contains 3
        cout << Q.get(); //Prints Queue empty
    Cosole: 12 Queu empty and 7624652????
    I want output: 12 Queue empty.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Your function returns an int and outputs it to cout. You have to return something from that function.

    You should check the queue to see if it is empty before calling Q.get() in your main code, and output "Queue empty" if it is empty there instead of inside the function.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    50
    Quote Originally Posted by akaile View Post
    Code:
    int MyQueue::get() 
    {
    	if ( empty() == false ) //queue not empty
    	{
    		top--;
    		return IArr[top1++]; //input result queue
    	}
    	else
    		cout << " Queue empty "; //queue rong
    }
    main()
    Code:
    MyQueue Q;
        Q.put(1);	//Queue contains 1
        Q.put(2);	//Queue contains 1 2
        cout << Q.get(); //Prints 1, queue contains 2 3
        cout << Q.get(); //Prints 2, queue contains 3
        cout << Q.get(); //Prints Queue empty
    Cosole: 12 Queu empty and 7624652????
    I want output: 12 Queue empty.

    Your function is returning an int, and since you don't have a return for all paths, it spits out garbage.
    Code:
    int MyQueue::get() 
    {
    	if ( empty() == false ) //queue not empty
    	{
    		top--;
    		return IArr[top1++]; //input result queue
    	}
    	else
           {
    		cout << " Queue empty "; //queue rong
                    //If you don't have a return here, then the function returns a value on it's own.
           }
    }
    What I would do, is instead have get() return a bool, and pass a parameter by reference which would be where you get your value from. Such as:


    Code:
    bool MyQueue::get(int &nRef) 
    {
    	if ( empty() == false ) //queue not empty
    	{
    		top--;
    		nRef = IArr[top1++]; //input result queue
    		return(true); //There was something here
    	}
    	else
            {
    		cout << " Queue empty "; //queue rong
    		return(false);
            }
    }

    Code:
    MyQueue Q;
        Q.put(1);	//Queue contains 1
        Q.put(2);	//Queue contains 1 2
        int nTemp(0);
    
        while(Q.get(nTemp) )    //While Q is not empty...
        {
              cout << nTemp; 
        }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pushing a Queue onto a Stack?
    By MiroMage in forum C Programming
    Replies: 5
    Last Post: 10-14-2008, 09:23 PM
  2. Queue with templates
    By pobri19 in forum C++ Programming
    Replies: 6
    Last Post: 09-23-2008, 06:06 AM
  3. Pathfinding AI? (Not the A* Algorithim)
    By harryP in forum C++ Programming
    Replies: 22
    Last Post: 08-01-2003, 02:32 PM
  4. queue / stack
    By smd in forum C++ Programming
    Replies: 2
    Last Post: 07-26-2002, 01:30 PM
  5. help with queues
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-21-2002, 09:09 PM