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;
}
}