bool get_XY (long *GT_X, long *GT_Y, int *option, int *loops)
{
clock_t start, finish;
double duration;
int opt;
int loop;
/* option value meaning:
0 = do loop number count
1 = turn off loop counting, activate automated click
2 = reset
*/
opt = *option;
switch (opt)
{
case 0: //first run or reset
duration = 0;
loop = 0;
opt = 1;
start = clock();
goto for_loop;
//reset
break;
case 1: //find no. of loops for specific time
loop++;
goto for_loop;
//find loops
break;
case 2: //do for condition loop
loop = *loops;
goto normal;
//do for loop
break;
default: //Other values means error
cout << "\nError ocurred in get XY: " << GetLastError () << endl;
flag = FALSE;
goto end;
}
normal:
for (int j = 0; j < loop ; j++)
for_loop:
{
cout << "counter to number of loops: "<< j << "\t" << "target: " << *loops << "\r";
if (cursor_move (GT_X, GT_Y) == TRUE)
flag = TRUE;
else
flag = FALSE;
if (opt == 2)
{
finish = clock(); //Timer stops
duration = (double)(finish - start) / CLOCKS_PER_SEC; //Duration in secs
if (duration == 1.5)
{
opt = 3;
goto end;
}
}
} //end for loop
end:
*option = opt;
*loops = loop;
if (flag)
return TRUE;
elsereturn FALSE;
}
That is my code. this part of code is to try to time a automated click for mouse.
in theory the code should work like this:
first pass: sets option, sets loop value. Both should be saved in the int main for use when doing second pass.
2nd pass: use values obtained already and skip redoing what 1st pass has done.
the rest should be the same as 2nd
please let me know u there is any part of the code u dont understand.
thanks for your help
ps the code is not complete that is why there is a reset case 0:
1) This use of goto is terrible. Get out of the habit of using goto. Code like this is incredibly difficult to follow
2) You shouldn't have second passes to a function that do two entirely different things. If the function does three different things, then split it up into three different functions.
Other problems include:
line 56: You should refrain from using == or != when dealing with doubles/floats. Due to precision imprefections caused by them being floating point, they hardly ever match a value EXACTLY and thus these conditions may fail/succeed when you don't expect. IE: 2.0 - 0.5 does not necessarily equal 1.5. You might get 1.49999999999999999999999.
The problem you ask about:
line 42: your 'for_loop' label skips over the actual for loop and just goes to the control block following it, which mean j is never initialized (since it's initialized in the for loop, which you're skipping over). This is why it's some weird random number.
thanks, but there isnt another way that i can think of to do my program without it doing this.
my program is to read input data setting the position of cursor and using an automated click when the positions of the cursor are same for 1.5 sec, in this case.
anyway going back to the problem, when i ignore for_loop and hav all cases going to normal, j still does - 8million. for some reason.
I don't see anything else that could be corrupting j -- I thought for sure that would fix it.
Did you just change the goto lines -- or did you try actually removing the for_loop label? Maybe having the label directly after the for loop is screwing it up somehow.
ok now i have a new problem, my code doesnt seem to like checking if the cursor position is still in the same area after 1.5 seconds later and just doing automated click whenever it passes it.