# What the? Loop repeating to much?

• 02-03-2006
Blackroot
What the? Loop repeating to much?
(Borland C++ free command line compiler, WinXP)
Ok, I'm switching all my functions so they use less code to read characters on a console screen. But its repeating to the max value of a short when given a negative. I tried using unsigned integers, I tried using signed integers. (Which one can go negative?) I tried using SHORTs, I dont get why its looping so much when passed a negative.

Heres my code:

Code:

char ReadPos(SHORT x, SHORT y, SHORT ix, SHORT iy) {
COORD Pos;
char EEE[1];
SHORT int v;

Pos.X = x;
Pos.Y = y;

for(v=0; v > ix; v++)
Pos.X--;

for(v=0; v > iy; v++)
Pos.Y--;

for(v=0; v < ix; v++)
x++;

for(v=0; v < iy; v++)
x++;

cout << x;
cout << y;

return EEE[0];
}

(HANDLE hOt is declared globaly.)

Not built in the best possible way, but it works when passed positive values.

And my call:
Code:

I'm getting very confused, It cant be my for's that are conflicting because that would be illogical... :confused:

Thank you!
• 02-03-2006
ZuK
Don't understand what you are doing but this looks strange
Code:

for(v=0; v < iy; v++)
x++; // shouldn't it be y++ ??

Kurt
• 02-03-2006
7stud
Quote:

I dont get why its looping so much when passed a negative.
Given your function signature and function call here:
Code:

char ReadPos(SHORT x, SHORT y, SHORT ix, SHORT iy)

That means ix = -1, when you get here:
Code:

for(v=0; v > ix; v++)
which gives you:
Code:

for(v=0; v > -1; v++)
The first time through the loop v is set = 0, and since v is greater than -1, the loop executes. v++ then increments v to +1, and that's greater than -1, so the loop executes again. The result is your loop keeps incrementing v to bigger and bigger positive numbers, while your loop conditional says to keep executing the loop as long as v is greater than -1. So, the loop keeps executing until v hits +infinity or your program aborts.
• 02-03-2006
dwks
You probably meant to put v-- instead of v++.
• 02-03-2006
Blackroot
I guess my logic was dumb -,-. I should have done the replacement myself to see what was going on. I'll fix this, thank you guys!