Thread: Help with Char Arrays in my game

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    16

    Question Help with Char Arrays in my game

    Code:
        int enemyNumber = 1;
        char enemy[] = "Dragon";
        int enemyHealth = 500;
        int enemyMagic = 0;
        int enemyAttackMin = 50;
        int enemyAttackMax = 60;
        char enemySpell[];
        int costOfSpell = 1;
        int enemyMagicMin;
        int enemyMagicMax;
    This is the initial enemy, Dragon. Once defeated, I change the enemy to Mad Golfer. Note that Dragon cannot use spells, hence his lack of a spell name. Here is the "changing code":

    Code:
              enemyNumber = 2;
              char enemy[] = "Mad Golfer";
              enemyHealth = 300;
              enemyMagic = 14;
              enemyAttackMin = 5;
              enemyAttackMax = 20;
              char enemySpell[] = " throws his club at you!";
              costOfSpell = 1;
              enemyMagicMin = 70;
              enemyMagicMax = 100;
    The thing is, the interger values adjust while the strings stay the same. I can't get them to change! The program displays Dragon as the name of the enemy instead of Mad Golfer. What am I doing wrong?
    The Random Plane
    My website of original old school games.

  2. #2
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    well for one thing, you are redeclaring your strings when you change them, that in itself is bad, and I don't see how it even compiles.

    In your change code, take out the "char" prefixes. See if that helps.

    Also, you dont need [] (brackets) when you redefine the string, just type:

    varName = "string";
    My Website

    "Circular logic is good because it is."

  3. #3
    I am he who is the man! Stan100's Avatar
    Join Date
    Sep 2002
    Posts
    361
    how about sticking that in a class too.
    Stan The Man. Beatles fan

    When I was a child,
    I spoke as a child,
    I thought as a child,
    I reasoned as a child.
    When I became a man,
    I put childish ways behind me"
    (the holy bible, Paul, in his first letter to the Cor. 13:11)

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    16
    I'm afraid I've tried that already. I get a compiling error--the array sizes are missing.

  5. #5
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    if you want to use "strings" that way, look into the string class (#include <string>) rather than c-strings. you cant arbitrarily change the size that way

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Also, you dont need [] (brackets) when you redefine the string, just type:

    varName = "string";
    I fail to see how that would work, considering the fact that varName is a fixed-length character array. The only reliable way to 'assign' strings to character arrays (i.e. c-strings) is to use strcpy(). A much better alternative, as Perspective suggested, is to use the std::string class, which CAN be assigned with the = operator and resized at will.
    Just Google It. √

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

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    16
    Thank you so much for the help! I'll look into it. I found another way around it since the enemies in my game would be too generic with the same pattern of behaviors. I will look at the string class and keep that in mind.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 08-11-2008, 11:02 PM
  2. C Programming 2d Array Question
    By jeev2005 in forum C Programming
    Replies: 3
    Last Post: 04-26-2006, 03:18 PM
  3. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  4. AnsiString versus char *
    By Zahl in forum C++ Programming
    Replies: 35
    Last Post: 10-16-2002, 08:38 PM
  5. errors in class(urgent )
    By ayesha in forum C++ Programming
    Replies: 1
    Last Post: 11-10-2001, 10:14 PM