Thread: Damned bool function

  1. #1
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168

    Damned bool function

    Im trying to make a function to determine whether an attack succeeds in a little game im making. The function is shown below

    Code:
    bool attack(Character human, Character orc, int turn)
    {
         int temp;
         bool outcome;
    
         switch (turn)
         {
                case 0:
                     temp = ((rand() % 30) + 1);
    
                     if (human.GetAgility() >= temp)
                        outcome = true;
                     else
                        outcome = false;
                     break;
                case 1:
                     outcome = false;
                     break;
         }
    
    return outcome;
    }
    There is no fancy code where the function is used at the minute, so there's no point posting that too.

    Turn is set to 0, hence why there is only one case at the moment as i tested it and it keeps returning true, and i have no idea why! There are no compile errors, just the incorrect return!

    human.GetAgility() simply returns the agility of the human character class

    Any ideas as to why this keeps happening would be greatly appreciated, as i've been looking at it and pulling my hair out for a couple of evenings over this.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Did you seed the random number generator with srand()?

    FAQ > How do I... (Level 1) > Generate random numbers?

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Could your function be pared down to this?
    Code:
    bool attack(Character human, Character orc, int turn)
    {
       return !turn ? human.GetAgility() >= rand() % 30 + 1 : false;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    Swoopy: yeah i did seed the number generator

    Dave: i have no idea what that code you just wrote does, lol , i've only been doing C++ a couple of months

  5. #5
    Chief Code Coloniser!
    Join Date
    Apr 2005
    Posts
    121
    Looks like it's a bit pointless having the orc parameter since it's not used

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Use a debugger to find out the values of temp and human.GetAgility(). Use that information to continue your debugging.

  7. #7
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Not sure if this would fix your problem, but here are some things I've noticed:
    1) outcome is not initialized
    2) you have no default case in your switch statement.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  8. #8
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    Quote Originally Posted by TheColonial
    Looks like it's a bit pointless having the orc parameter since it's not used
    I'm planning on using it in the 'case 2' statement, i was just intending on getting case 1 working first :P

    Thanks for the ideas guys, ill continue to play with it and see what i come up with

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing params between managed c++ and unmanaged c++
    By cechen in forum C++ Programming
    Replies: 11
    Last Post: 02-03-2009, 08:46 AM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM