You can check wich part of your function is executed if you place printf statements in each part.
Or, you can tackle the problem like a
real programmer would with a debugger.
Figure out how the Gamestudio debugger works and trace through your code a little bit. It'll soon be clear what the problem is if you do that.
I can see the logical error you have in your code as I write this, but I think it's important that you learn to fix issues such as these yourself, so I leave this up to you as an exercise.
