Wysota's solution does what your pseudo-code does. Think about what Wysota gave you and adapt.

Two hints: more than one loop, and find a good introductory programming text.