What the? Loop repeating to much?

This is a discussion on What the? Loop repeating to much? within the C++ Programming forums, part of the General Programming Boards category; (Borland C++ free command line compiler, WinXP) Ok, I'm switching all my functions so they use less code to read ...

  1. #1
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379

    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) {
     DWORD reader;
     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;
    
     ReadConsoleOutputCharacter(hOt, &EEE[0], 1, Pos, &reader);
    
     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:
    SomeChar[0]=ReadPos(Pos.X, Pos.Y, -1, 0);
    I'm getting very confused, It cant be my for's that are conflicting because that would be illogical...

    Thank you!
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    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

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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) 
    
    SomeChar[0]=ReadPos(Pos.X, Pos.Y, -1, 0);
    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.
    Last edited by 7stud; 02-03-2006 at 04:32 AM.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    You probably meant to put v-- instead of v++.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    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!
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  2. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 06:19 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 08:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21