0

I'm brand new to C++, and I've been trying to make a simple game. I want a system for status effects the player can apply to the enemy, so I made an array of strings that can hold 5 elements, like "burning" or "poisoned" etc. When I tested this, however, it seemed to always apply the status effect (in this case, burning) to the first AND second status "slot". I want it to only apply to my first empty slot. I took this portion of the code out and expanded it from a for loop to an if statement, and this time I outputted the value of statusEffect and it gave me 0x1000020f0. I have no idea what that even is.

Here's my code, I'm working on Xcode if that helps.

int statusEffectDuration[5] = {0, 0, 0, 0, 0};
string statusEffect[5] = {"NONE", "NONE", "NONE", "NONE", "NONE"};

// Function for applying status effects
void applyStatusEffect(string appliedEffect, int statusDuration) {

    if (statusEffect[0] == "NONE") { // If first status effect is nothing then changes first status to "EFFECT"
        statusEffect[0] = appliedEffect;
        statusEffectDuration[0] = statusDuration;
    }
    else if (statusEffect[1] == "NONE") { // Same as first, but if second status is nothing then changes second status to "EFFECT"
        statusEffect[1] = appliedEffect;
        statusEffectDuration[1] = statusDuration;
    }
    else if (statusEffect[2] == "NONE") { // Third
        statusEffect[2] = appliedEffect;
        statusEffectDuration[2] = statusDuration;
    }
    else if (statusEffect[3] == "NONE") { // Fourth
        statusEffect[3] = appliedEffect;
        statusEffectDuration[3] = statusDuration;
    }
    else if (statusEffect[4] == "NONE") { // Fifth
        statusEffect[4] = appliedEffect;
        statusEffectDuration[4] = statusDuration;
    }
    else if (statusEffect[5] == "NONE") { // Sixth
        statusEffect[5] = appliedEffect;
        statusEffectDuration[5] = statusDuration;
    }
}

int main() {

    applyStatusEffect("EFFECT", 5); // "EFFECT" is the effect that is given, 5 is duration of effect

    // Displays status effect and effect duration
    cout << statusEffect; // Displays 0x1000020f0
    cout << "\n";
    cout << statusEffectDuration; // Displays 0x1000020f0

    return 0;
}

EDIT: I'm dumb. I was printing the address. But the problem with it giving the value to the first two empty spaces instead of one is the original reason I posted this.

  • 3
    `// Sixth` there are only 5 elements in your array. – KamilCuk Dec 11 '19 at 21:22
  • I suggest you not to use raw arrays. If you learn about `std::vector` and/or `std::array` and how to use it instead, you will be much less surprised by the unintuitive behavior of raw arrays. – walnut Dec 11 '19 at 21:24
  • I know that there are only 5 elements, but I was too lazy to change it when I was solving another problem already :P the problem is that it applies the status effect to the first and second "slot", when I only want it to apply to the first. – Charlesistaken Dec 11 '19 at 21:27
  • @Charlesistaken Trying to use an array outside its bounds causes [*undefined behavior*](https://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior). That means that your whole program may start doing anything at all. You loose any guarantee that it will do anything sensible. Never be lazy about something like this. You should also not ask two questions at once. – walnut Dec 11 '19 at 21:30
  • Welp, sorry about that. The being lazy thing and also the asking two questions part. Thanks for the help though! – Charlesistaken Dec 11 '19 at 22:29

1 Answers1

0

You are telling the computer to print the address of the array.
To print the contents of the array, you'll need to use a loop:

for (i = 0; i < 5; ++i)
{
  cout << statusEffect[i] << "\n";
}
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154