View Single Post
  #2 (permalink)  
Old 01-28-2008, 04:28 PM
jelly's Avatar
jelly jelly is offline
Member
 
Join Date: Jan 2008
Location: Somerset, UK
Posts: 46
jelly is on a distinguished road
Your problem is caused by

Code:
for(int i=0;i<stack.size();i++)
you are getting the size of the stack each time round but your index is increasing in value while the stack size is decreasing so:

loop 1, i = 0, stack.size() = 4, you print values
loop 2, i = 1, stack.size() = 3, you print values
loop 3, i = 2, stack.size() = 2, and you drop out of the loop

take the original stack size first and then use that for the loop check, i.e.

Code:
int original_stack_size = stack.size(); for(int i=0; i< original_stack_size; i++)
as well as fixing the problem it is also more efficient
__________________
-- Hope that helps
Reply With Quote