Thread: Help on how to properly return a value

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    32

    Help on how to properly return a value

    I need this function to return the value in the 'current' location, then decrement the current value.

    My question is, how do I return, but then still decrement after that return in the same function? Is it possible?

    Current code:
    Code:
    int Stack::pop()
    {
    	if(current == 0)
    		return 0;
    	else
    	{
    		list.DeleteItem(current);
    		current--;
    		return list.GetItem(current);
    	}
    }
    What I need it to do:
    Code:
    int Stack::pop()
    {
    	if(current == 0)
    		return 0;
    	else
    	{
    		Delete the Current Value
                    Return the value being Deleted
                    Decrement Current
         
                   //return list.DeleteItem(current);
                  // current--;
                  //But this won't work b/c it decrements after return?!
    
    	}
    }

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    int val = *current;
    int* temp = current;
    current--;
    list.DeleteItem(temp);
    return val;

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    32
    Current is the index of the top of the stack.

    I need to Delete the value that is in the current location, then return that value, then decrement current.


    return list.DeleteItem(current--);
    Won't that just delete current - 1? Thus deleting the wrong item..

    To get a value with my functions you need to call: get("location") ---> get(current)

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    32
    int val = *current;
    int* temp = current;
    current--;
    list.DeleteItem(temp);
    return val;
    Hmmm.. Using those pointers in my pop() function, bit confused.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    As mentioned in the other thread, you have to get the value before you delete the item, and before you decrement the location. Look at 7stud's code as an example and hint, not exact syntax.

    >> Won't that just delete current - 1
    No, DeleteItem(current--) is a fancy way of deleting the item at location current, then decrementing current by 1. If you did it this way: DeleteItem(--current), then it would delete current - 1. It's just pre-increment vs. post-increment.

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    32
    So the only solution is going to involve pointers.....?

    No, DeleteItem(current--) is a fancy way of deleting the item at location current, then decrementing current by 1. If you did it this way: DeleteItem(--current), then it would delete current - 1. It's just pre-increment vs. post-increment.
    -Never knew, thats SWEET!

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    No... you don't need to use pointers at all. Ignore the syntax, pay attention to the flow of the example and the flow of my explanation. Like I said, it is pretty simple.

    >> Delete the Current Value
    >> Return the value being Deleted
    >> Decrement Current

    That is not quite correct obviously because you cannot return a value and then decrement current. Just try re-arranging those things, maybe adding one more step, and you'll get it.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Hmmm.. Using those pointers in my pop() function, bit confused.
    Are you aware that you can have more than one pointer pointing to something? You can have 100 pointers all pointing at the top of the stack. So, if you move one of those pointers, then 99 of them will still point to the top of the stack. Furthermore, you can dereference any of those 99 pointers to get the value at the top of the stack, and you can delete any of those 99 pointers to delete the top of the stack.

    My code example happens to use only two pointers that point to the top of the stack: current and temp. I moved current, so that left me with temp pointing to the top of the stack. Then I dereferenced temp to get the value at the top of the stack, and then I deleted temp which deleted the memory at the top of the stack. Then I returned the value at the top of the stack.

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    So the only solution is going to involve pointers.....?
    Isn't current a pointer?

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    No... it's an int location (kind of like a 1-based array index). See dld333's other thread for full code.

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    See dld333's other thread for full code.
    I'll check out instead. Geeze, I hate when people do that.

  12. #12
    Registered User
    Join Date
    Sep 2005
    Posts
    32
    I'll check out instead. Geeze, I hate when people do that.
    Sorry, I thought new post was correct action. Won't happen again.

  13. #13
    Registered User
    Join Date
    Sep 2005
    Posts
    32
    Like I said, it is pretty simple.
    Wow, I don't know what I was thinking.. I was making that a lot harder than it had to be.

    Thanks a lot guys.

    Daved - you are the man, thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New string functions
    By Elysia in forum C Programming
    Replies: 11
    Last Post: 03-28-2009, 05:03 AM
  2. C or C++
    By AcerN30 in forum Game Programming
    Replies: 41
    Last Post: 05-30-2008, 06:57 PM
  3. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  4. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM
  5. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM