It's hard to tell you what's wrong without a real example of the input to the program.