You are correct that there were problems in the OP's code. Since the OP also showed desired output, I was expecting that posted solutions would actually produce that output. Yours and dreadpiratepeter's solutions produced the desired output, but Jstrom's did not.