Your problem is caused by
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.
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