What exactly is the problem? We can't read your mind.
What input did you give, what output did you get, did it match what you expected, and if not what did you expect?
What line of code does it start going wrong? (If you find this, then you stand a good chance of fixing the problem. To find it, you can add printf/cout statements to the code to display values of variables; this will show you if the code is really doing what you expect)