Thread: selecting item from a for loop

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    26

    selecting item from a for loop

    quick question that is driving me insane..

    I have a for loop that loops from 9 to 12 to check if the mouse cursor is above any of the items with index between 9 and 12..

    Code:
    for (int i = 9; i <= 12; i++)
            {
    if (IntersectsModel(Crusor, Item[i]){
       ItemIndex = i;
    }
    
    
    }
    However this method always returns ItemIndex as being 12 no matter which object the cursor is actually over. How can I make it return the proper item index? I know the problem is with the for loop logic and not the actual IntersectModel function.

    Thanks.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    I may get flamed for saying this, but you could put a break statement right after you assign the value to your ItemIndex variable. it would break out of the loop and continue with your program.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    26
    Quote Originally Posted by Elkvis View Post
    I may get flamed for saying this, but you could put a break statement right after you assign the value to your ItemIndex variable. it would break out of the loop and continue with your program.
    This way it stops at the first index and always returns a 9

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Brigs76 View Post
    This way it stops at the first index and always returns a 9
    That would be because you put the break in the main body of the for-loop, instead of inside the if-statement.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Is it possible that IntersectsModel is faulty and always returns true?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Please pick an indentation style and stick to it. e.g.:
    Code:
    for (int i = 9; i <= 12; i++) {
        if (IntersectsModel(Crusor, Item[i]) {
            ItemIndex = i;
        }
    }
    Code:
    for (int i = 9; i <= 12; i++)
    {
        if (IntersectsModel(Crusor, Item[i])
        {
            ItemIndex = i;
        }
    }
    Quote Originally Posted by Brigs76 View Post
    However this method always returns ItemIndex as being 12 no matter which object the cursor is actually over. How can I make it return the proper item index? I know the problem is with the for loop logic and not the actual IntersectModel function.
    Then what you know is wrong, because the only way this for loop could do that is if IntersectModel were faulty.
    Also, you spelt Cursor wrong.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Immediate programming help! Please!
    By xMEGANx in forum C++ Programming
    Replies: 6
    Last Post: 02-20-2008, 12:52 PM
  2. Retail Outlet Managment System - the 4th
    By Presidentofusa in forum C Programming
    Replies: 3
    Last Post: 11-10-2007, 10:44 PM
  3. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM