The loop appears to be about right except you need to save the value of pos so that you can use it after the loop finished. The current code destroys the value of pos.
Hmm... I think it is more of a compilation error.
It would probably consider int n ;
and int pos;
as the redeclaration and flag errors.