-2

I am trying to create a health damage meter for the text-based RPG I am working on. I want the HP to get to 0. However I keep getting an output like this:

opponents hp is now 70
will you attack?
1. yes
2.No
1
hp has been reduced to50
opponents hp is now 50
will you attack?
1. yes
2.No
1
hp has been reduced to50
opponents hp is now 50
will you attack?
1. yes
2.No
1
hp has been reduced to50
opponents hp is now 50
will you attack?
1. yes
2.No
1
hp has been reduced to50
opponents hp is now 50
will you attack?
1. yes
2.No
1
hp has been reduced to50
opponents hp is now 50
will you attack?
1. yes
2.No

Bearing in mind I haven't done anything with the dice until normal damage works perfectly, is there any way to do it without using multiple if statements?

Here's the code:

#include <iostream>
#include <ctime>
#include <stdlib.h>

using namespace std;

void Dice();
void MainMenu();

//global variable
int result = 0;
//

void Dice() {
//dice functionality
    srand( time( NULL ) );
    int number = 100;
    result = rand() % number + 1;
    ///
}

void MainMenu() {//variables
    int HP = 70;
    int HPtotal = 0;
    int damage[7] = { 20, 20, 20, 20, 20, 20, 20 };
    int choice;
    //

    HPtotal = HP;

    // repeat infinetly
    for ( int i = 0; i < 999; i++ ) {
    //main menu
        cout << "opponents hp is now " << HPtotal << endl;
        cout << "will you attack?" << endl;
        cout << "1. yes" << endl;
        cout << "2.No" << endl;
        cin >> choice;
        //

        //damage system
        if ( choice == 1 ) {
            HPtotal = HP - damage[i];

            cout << "hp has been reduced to" << HPtotal << endl;

            //while HPtotal is less than 70

            //END OF SYSTEM
        }

        if ( choice == 2 ) {//if pressed 2
            cout << "END OF PROGRAM BAKA" << endl;
            system( "PAUSE" );
        }//end if
    }//end for
}

int main() {
    MainMenu();
    Dice();
}
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
ninten
  • 13
  • 4
  • 3
    `HPtotal = HP - damage[i];` shouldn't that be `HPtotal = HPtotal - damage[i];` (which is `HPtotal -= damage[i];`)? And why have both a `HPtotal ` and `HP` variable? – Blaze Dec 05 '19 at 10:37
  • 1
    Unrelated see: Why not to use [System calls](https://stackoverflow.com/questions/1107705/systempause-why-is-it-wrong) and [Why is using namespace std bad](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – ChrisMM Dec 05 '19 at 10:40
  • 1
    1) ```for (int i=0; i < 999; i++)``` is not repeat infinitely. 2) Your ```damage``` array only has 7 elements, so after the first seven choices you choose to attack you will access an out-of-bounds element. – AshleyWilkes Dec 05 '19 at 10:45
  • Also unrelated: `srand` should be called _once_ only at startup. – Lukas-T Dec 05 '19 at 11:02

1 Answers1

-1

your program has a problem HPtotal = HP - damage[i]; it should be HPtotal = HPtotal - damage[i];

and for your will go on indefinitely you should use an while loop

int i = 0;
while(true)
{
//main menu
    cout << "opponents hp is now " << HPtotal << endl;
    cout << "will you attack?" << endl;
    cout << "1. yes" << endl;
    cout << "2.No" << endl;
    cin >> choice;
    //

    //damage system
    if ( choice == 1 ) {
        HPtotal = HPtotal - damage[i];

        cout << "hp has been reduced to" << HPtotal << endl;

        //while HPtotal is less than 70
        //END OF SYSTEM
    }

    if ( choice == 2 ) {//if pressed 2
        cout << "END OF PROGRAM BAKA" << endl;
        system( "PAUSE" );
    }//end if
}//end of while loop

and to fix get the i back just add the i on top of the while loop

and if you are wondering why isn't your hp being reduced its because HPtotal = HP - Damage[i] and HP has the value of 70. 70 - 20 = 50 but because you are not changing the value of the HP its all ways 70 and you are just setting HPtotal's value to 50

Rixu-chan
  • 28
  • 4