0

I'm a C++ student who's making an RPG game with the limited knowledge I have (just learned about classes today) and I've got most of the details worked out. One thing has been making my head itch, though, and it's the fact that when I'm having a party member's health values changed by their own team, the numbers constantly go many digits into the negatives. What should be basic subtraction is a mess that's driving me insane!

Some context: "player" is the one doing the attack, while "players" is the one taking the attack. These can heal too.

Expected result: player2.health -= player.att1.damage - player2.armor

if player2.health = 15 and player.att1.damage = 3, player2.health should be 12

instead, it can reach new lows of -858993450 HP

Code:

void pvp(Party &player, Party &player2) {
    int select, flinch = 2;
    if (player.effect.flinch)
        flinch = rand() % 2;
    moveshow(player);
    cin >> select;
    switch (select) {
    case 1: {
        srand(time(NULL));
        string re;
        int t = rand() % 100 + 1;
        int r = rand() % player.att1.hitcount + 1;
        if (r == 1)
            re = " time";
        else
            re = " times";
        if (t < (player.att1.accuracy - player.att1.blindness + player.att1.deadeye) & flinch != 1) {
            if (player.att1.damage < 0) {
                cout << player.name << player.att1.action << player2.name << r << re << ", healing " << abs(player.att1.damage * r) << " health.\n";
            }
            else if (player.att1.damage == 0) {
                cout << player.name << player.att1.action << player2.name << r << re << endl;
            }
            else
                cout << player.name << player.att1.action << player2.name << r << re << ", dealing " << player.att1.damage * r << " damage.\n";
            player2.health -= (player.att1.damage * r) - player2.armor;
            cout << player2.name << ": " << player2.health << " HP\n";
            pvpinflict(player, player2, select);
            player.att1.pp--;
        }
        else if (flinch == 1)
            cout << player.name << " flinched and couldn't move!\n";
        else {
            cout << player.name << " missed!\n";
            player.att1.pp--;
        }

        break;
    }
    case 2: {
        srand(time(NULL));
        string re;
        int t = rand() % 100 + 1;
        int r = rand() % player.att2.hitcount + 1;
        if (r == 1)
            re = " time";
        else
            re = " times";
        if (t < (player.att2.accuracy - player.att2.blindness + player.att2.deadeye) & flinch != 1) {
            if (player.att2.damage < 0) {
                cout << player.name << player.att2.action << player2.name << r << re << ", healing " << abs(player.att2.damage * r) << " health.\n";
            }
            else if (player.att2.damage == 0) {
                cout << player.name << player.att2.action << player2.name << r << re << endl;
            }
            else
                cout << player.name << player.att2.action << player2.name << r << re << ", dealing " << player.att2.damage * r << " damage.\n";
            player2.health -= (player.att2.damage * r) - player2.armor;
            cout << player2.name << ": " << player2.health << " HP\n";
            pvpinflict(player, player2, select);
            player.att2.pp--;
        }
        else if (flinch == 1)
            cout << player.name << " flinched and couldn't move!\n";
        else {
            cout << player.name << " missed!\n";
            player.att2.pp--;
        }

        break;
    }
    case 3: {
        srand(time(NULL));
        string re;
        int t = rand() % 100 + 1;
        int r = rand() % player.att3.hitcount + 1;
        if (r == 1)
            re = " time";
        else
            re = " times";
        if (t < (player.att3.accuracy - player.att3.blindness + player.att3.deadeye) & flinch != 1) {
            if (player.att3.damage < 0) {
                cout << player.name << player.att3.action << player2.name << r << re << ", healing " << abs(player.att3.damage * r) << " health.\n";
            }
            else if (player.att3.damage == 0) {
                cout << player.name << player.att3.action << player2.name << r << re << endl;
            }
            else
                cout << player.name << player.att3.action << player2.name << r << re << ", dealing " << player.att3.damage * r << " damage.\n";
            player2.health -= (player.att3.damage * r) - player2.armor;
            cout << player2.name << ": " << player2.health << " HP\n";
            pvpinflict(player, player2, select);
            player.att3.pp--;
        }
        else if (flinch == 1)
            cout << player.name << " flinched and couldn't move!\n";
        else {
            cout << player.name << " missed!\n";
            player.att3.pp--;
        }

        break;
    }
    }
}```
Mat
  • 202,337
  • 40
  • 393
  • 406
Paul_00
  • 11
  • 3
  • 1
    `-858993450` my guess is one or more of your variables are uninitailized. – drescherjm May 14 '20 at 20:05
  • 1
    Step through the code using a debugger while checking how the variables change. – darcamo May 14 '20 at 20:25
  • Like @drescherjm comment, it is possible that a variable is not initialized. We can really see any variable types here, so also check overflowing. and my eyes are bleeding. use library please. are you sure about player2.armor value btw. – sariug May 14 '20 at 20:31
  • Since you mentioned Visual Studio you do have a good debugger. Please learn how to use it effectively to help solve problems like this. There is a very good tutorial here: [https://learn.microsoft.com/en-us/visualstudio/debugger/getting-started-with-the-debugger-cpp?view=vs-2019](https://learn.microsoft.com/en-us/visualstudio/debugger/getting-started-with-the-debugger-cpp?view=vs-2019) – drescherjm May 14 '20 at 21:02
  • 2
    -858993450 is a good number to use as an example. In hex it is CCCCCCD6, which is CCCCCCCC + decimal 10. When you see a number like this that is too regular to be dumb luck or spells out (or almost spells out) a word or phrase (DEADBEEF is popular) it is probably the program trying to tell you something. Look it up in [a list of common debugger codes](https://en.wikipedia.org/wiki/Magic_number_(programming)#Debug_values). CCCCCCCC is often used to mark uninitialized stack memory, leading to @drescherjm 's guess. – user4581301 May 14 '20 at 21:27
  • Unrelated but useful reading because it will probably solve a few bugs you'll find later: [srand() — why call it only once?](https://stackoverflow.com/questions/7343833/srand-why-call-it-only-once) – user4581301 May 14 '20 at 21:29
  • Recommendation: You have a tone of duplicated code that only differs by which variables are being acted on. You can turn the duplicate code into a function and pass in the different variables. – user4581301 May 14 '20 at 21:33

0 Answers0