in "for" condition my j keeps on equalling -8million sthg!!??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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;
	else
		return 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:
oops sorry ignore the comment on option values, its not updated
0 = reset
1 = find loop
2 = normal operation
You have serious flow control problems.

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.

and thanks for the advise on line 56.

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.
went to sleep last night and woke up this morning with an idea, use do while() loop.

it works lol........ so smiple now

1
2
3
4
5
6
7
8
9
10
11
12
13
14
	start = clock();

	do
	{	
		if (cursor_control (GT_X, GT_Y) == TRUE)
			flag = TRUE;
		else
			flag = FALSE;
		
			finish = clock();		//Timer stops
			duration = (double)(finish - start) / CLOCKS_PER_SEC;		//Duration in secs
//		cout << "\t" <<duration << "\r";	
	} 
	while (duration < 1.5);


simple right?

wondering why i did the complimcated way.

thanks disch
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
bool get_XY (long *GT_X, long *GT_Y, int *Screen_Res_X, int *Screen_Res_Y)
{
	clock_t start, finish;
	double duration;

//DECLARATION OF VARIABLES for use in detecting automated click
	long lower_x, lower_y, upper_x, upper_y;

	//Setting lower and upper limit of cursor
	//For X axis
		if ( *GT_X -5 < 0 )
			lower_x = 0;
		else
			lower_x = *GT_X - 5;//end else if

		if (*GT_X + 5 > *Screen_Res_X )
			upper_x = *Screen_Res_X;
		else
			upper_x = *GT_X + 5;//end else if

	//For Y axis
		if ( *GT_Y -5 < 0 )
			lower_y = 0;
		else
			lower_y = *GT_Y - 5;//end else if

		if (*GT_Y + 5 > *Screen_Res_Y )
			upper_y = *Screen_Res_Y;
		else
			upper_y = *GT_Y + 5;//end else if
	
	start = clock();

	do
	{	
		if (cursor_control (GT_X, GT_Y) == TRUE)
			flag = TRUE;
		else
			flag = FALSE;
		
			finish = clock();		//Timer stops
			duration = (double)(finish - start) / CLOCKS_PER_SEC;		//Duration in secs
//		cout << "\t" <<duration << "\r";	
	} 
	while (duration < 1.5);

	if ((lower_x -1) < *GT_X < (upper_x +1))
	{
		if ((lower_y-1) < *GT_Y < (upper_y +1))
		{
			GT_automated_click (*GT_X, *GT_Y);
//			cout << "GT automated click check\r";
		}
	} //end if, ends automated click

	if (flag)
		return TRUE;
	else
		return FALSE;
}


this is using the simplified code for timer delay on checking new GT_x and GT_y axis position.

any help?

thanks
solved by changing if (x < y <z) to a if ( (x < y) && (y < z) )
Last edited on
Topic archived. No new replies allowed.