It's not obvious why it's crashing, but if it's crashing in CompareCursor, it's probably caused by bad indexing into cursor.
I'd suggest running it in a debugger. When it crashes, have a look at the stack and check the index values into cursor, then try to work out why they're wrong.
i altered the code and did the debugging again, had a look at the values it produces and they are fine. But when i run it like a normal, access violation happens again and its still mid > 200.
so i made a loop to fix the error, but that doesnt solve why mid exceeds 200.
i am trying to write a program that does automatic mouse clicks. so what i am trying to do is sample 200 mouse positions and compare the current, the earliest and one in between the two mouse positions.