same code, different result?!

This is a discussion on same code, different result?! within the Windows Programming forums, part of the Platform Specific Boards category; ok, these are the two sets of code: Code: if(7500 - (GetTickCount() - timeCreated) <= 0) return false; else return ...

  1. #1
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879

    same code, different result?!

    ok, these are the two sets of code:

    Code:
    if(7500 - (GetTickCount() - timeCreated) <= 0)
         return false;
    else
         return true;
    Code:
    int x = GetTickCount();
    x -= timeCreated;
    
    if(7500 - (x) <= 0)
         return false;
    else
         return true;
    I'm using it as a sort of timer for when to make rockets disappear - each gets called like this, inside of a loop. If it returns false, the rocket disappears ("rockets" is a std::vector):

    Code:
    for(int i = 0; i < rockets.size(); ++i)
    {
    if(!rockets[ i]->move())
         (...);
    }
    The only problem is, with the first set of code, only one rocket disappears after the designated 7.5 seconds; the rest keep going - but with the second set, they do all disappear after 7.5 seconds! Does anybody know why?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >if(7500 - (GetTickCount() - timeCreated) <= 0)

    GetTickCount() returns a DWORD, which I think is an unsigned long value. So the result of the subtraction is unsigned, so the result is alway > 0.

    Try either:
    if ((long) 7500 - (long)(GetTickCount() - timeCreated) <= 0)

    Or:
    if (GetTickCount() - timeCreated >= 7500)

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Also, timeCreated should be declared as:

    DWORD timeCreated;

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Ok, thanks!
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculator code help
    By morpheous11 in forum C Programming
    Replies: 4
    Last Post: 12-15-2008, 08:34 PM
  2. Help me debug this code
    By Blgihted in forum C Programming
    Replies: 4
    Last Post: 10-28-2005, 08:39 AM
  3. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 02:37 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM

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