0

Hello,
My friend and I were done with our work in math class, and threw together this little text-based rpg in like ten minutes. Anyway, we're having a small issue, everytime I press the "2" for "Fight!" the loop regenerates the random values and we mysteriously gain health that was lost in the last instance. I know this is the nature of the loop, but is there a simple way to store the temporary value of the remaining health?
Thanks!

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int wepchoice;
int questchoice;
float charhealth=100;
float enemyhealth=100;
int main()
{
    cout << "You approach a giant robot, that is capable of killing you with one hit, but instead uses mediocre weaponry to make the game last ." << endl;
    cout<<"Choose your weapon-there's only one"<<endl;
    cout<<"1-Ye Olde Dagger"<<endl;
    cin>>wepchoice;
    cout<<"You have chosen the dagger, it does random damage!"<<endl;
    cout<<"What do you want to do? Flee or Fight?"<<endl;
    cout<<"1. Flee"<<endl;
    cout<<"2. Fight"<<endl;
    cin>>questchoice;
    if (questchoice==1) {
    cout<<"HAHA you have failed, your village is now in flames."<<endl;
    }

if (questchoice==2) {
srand(time (NULL));
cout<<"The enemy is attacking you!"<<endl;
float enemyattack= rand() %100 + 1;
float remhealth=charhealth-enemyattack;
cout<<"You now have "<<remhealth<<" health left"<<endl;
cout<<"You are now going to attack"<<endl;
float charattack= rand() %100 + 1;
float remhealthenemy=enemyhealth-charattack;
cout<<"The enemy has "<<remhealthenemy<<" health left."<<endl;
cin>>questchoice;
}
 while (charhealth>0);


if (charhealth<0) {
cout<<"You're dead, you lsoe."<<endl;
}

if (enemyhealth<0) {
cout<<"Congratulations!  YOU HAVE SAVED THE VILLAGE!"<<endl;



}
}

Edited by Carpetfizz

3
Contributors
6
Replies
7
Views
4 Years
Discussion Span
Last Post by Carpetfizz
0

Line 30..
Every time the loop calculates, it calculates using your CharHealth which is still at 100!

float remhealth=charhealth-enemyattack;
charhealth = remhealth;   //Next time the loop starts, it will start with your current remaining health.

I "THINK" that's what you want. It's a bit difficult to understand your question but that's what I assume you mean. Try it and let me know how it goes.

Edited by triumphost

0

oh no way you beat me!!!
ok heres the code anyway for you tidied up and corrected

    #include <iostream>
    #include <ctime>
    #include <cstdlib>

    using namespace std;

    int wepchoice;
    int questchoice;

    float charhealth=100;
    float enemyhealth=100;

    float enemyattack;
    float remhealth;

    float charattack;
    //float remhealthenemy; not needed

    int main()
    {
        cout << "You approach a giant robot, that is capable of killing you with one hit, but instead uses mediocre weaponry to make the game last ." << endl;
        cout<<"Choose your weapon-there's only one"<<endl;
        cout<<"1-Ye Olde Dagger"<<endl;

        cin>>wepchoice;

        // Loop while character has health
        while (charhealth > 0)
        {
            cout<<"You have chosen the dagger, it does random damage!"<<endl;
            cout<<"What do you want to do? Flee or Fight?"<<endl;
            cout<<"1. Flee"<<endl;
            cout<<"2. Fight"<<endl;

            cin>>questchoice;

            if (questchoice == 1) 
            {
                cout << "HAHA you have failed, your village is now in flames." << endl;
                cout << "Keep attacking..." << endl;
            }

            if (questchoice == 2) 
            {
                srand((unsigned int)time(NULL));

                cout << "The enemy is attacking you!" << endl;

                enemyattack = (float)(rand() %100 + 1);
                charhealth  -= enemyattack;

                cout << "You now have "<< charhealth <<" health left" << endl;
                cout << "You are now going to attack" << endl;

                charattack = (float)(rand() %100 + 1);

                enemyhealth -= charattack;
                cout << "The enemy has " << enemyhealth << " health left." << endl;
            }

            if (charhealth <= 0) 
            {
                cout << "You're dead, you lsoe." << endl;
            }

            if (enemyhealth <= 0) 
            {
                cout<<"Congratulations! YOU HAVE SAVED THE VILLAGE!"<<endl;
            }
        }
    }
0

Hey triump, really appreciate you doing this! It works great, however, I had questions on what the unsigned int on line45 was. Also, what's going on on lines 49 and 55? Thanks again.

0

ok... maybe you dont need the (unsigned int) but it 'casts' the result of time fom time_t to something acceptable to srand and doesnt throw a warning in compilation. The same with the other lines you mentioned. I am casting the result to float explicitly. also in the lines after (line 50 and line 57) it basically mean var1 = var1 - var2; you just dont have to write the fist variable twice.
Happy to help...

0

Thanks, just did some reasearch on it, and I think I understand now. This worked out really well :D

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.