Trying to write an elegant code with do...while. please help