Question: C++ This program brings up the menu properly but exits immediately. I would like the user to choose 2 menu choices but when i run
C++ This program brings up the menu properly but exits immediately. I would like the user to choose 2 menu choices but when i run it now the computer chooses 2 to fight. It chooses vampire and harry potter right away and i want to be able to choose 2 to fight.
Can someone help me figure out how to make the options available prior to running these random battles and exiting. Thank you.
#include #include using namespace std;
class Roll { public: int num; int sides; };
class Creature { public : Roll attackPoints; Roll defensePoints; int armorPoints; int strengthPoints;
//geenrating srand seed for each creature once Creature() { srand((unsigned)time(0)); } virtual int attack(); virtual void defense(int attackPoints);
//generating rolldice for every creature with number of rolls and sides of dice as input int generateRollDice(int num, int sides) { int ans = 0; for(int i=0;i ans+=(rand()%sides)+1; } return ans; }
};
class Vampire : public Creature { public : Vampire() { cout<<"Vampire Selected"< attackPoints.num = 1; attackPoints.sides = 12; defensePoints.num = 1; defensePoints.sides = 6; armorPoints = 1; strengthPoints = 18; }
int attack() { int res = generateRollDice(attackPoints.num, attackPoints.sides); cout<<"Attack points Generated :"+res< return res; }
void defense(int points) { //if charm used.. No attackPoints. int defPoints = generateRollDice(defensePoints.num, defensePoints.sides); cout<<"Defense points Generated : "+defPoints< if(charm()) { cout<<"Charm used by Vampire. Saves himself."< cout<<"Strength Points remaining : "+strengthPoints< }
int finalDefensePoints = points - defPoints - armorPoints; if(finalDefensePoints <= 0) { finalDefensePoints = 0; cout<<"Defense is stronger."; } strengthPoints -= finalDefensePoints; if(strengthPoints <= 0) strengthPoints = 0; }
private: //50% chance geenration random. int charm() { return rand()%2; } };
class Barbarian : public Creature { public : Barbarian() { cout<<"Barbarian Selected"< attackPoints.num = 2; attackPoints.sides = 6; defensePoints.num = 2; defensePoints.sides = 6; armorPoints = 0; strengthPoints = 12; }
int attack() { int res = generateRollDice(attackPoints.num, attackPoints.sides); cout<<"Attack points Generated :"+res< return res; }
void defense(int points) { int defPoints = generateRollDice(defensePoints.num, defensePoints.sides); cout<<"Defense points Generated : "+defPoints< int finalDefensePoints = points - defPoints - armorPoints; if(finalDefensePoints <= 0) { finalDefensePoints = 0; cout<<"Defense is stronger."; } strengthPoints -= finalDefensePoints; if(strengthPoints <= 0) strengthPoints = 0; } };
class BlueMen : public Creature { public : BlueMen() { cout<<"BlueMen Selected"< attackPoints.num = 2; attackPoints.sides = 10; defensePoints.num = 3; defensePoints.sides = 6; armorPoints = 3; strengthPoints = 12; }
int attack() { int res = generateRollDice(attackPoints.num, attackPoints.sides); cout<<"Attack points Generated :"+res< return res; }
void defense(int points) { int defPoints = generateRollDice(defensePoints.num, defensePoints.sides); cout<<"Defense points Generated : "+defPoints< int finalDefensePoints = points - defPoints - armorPoints; if(finalDefensePoints <= 0) { finalDefensePoints = 0; cout<<"Defense is stronger."; } strengthPoints -= finalDefensePoints;
//special functionality for blbue men. mob(finalDefensePoints);
if(strengthPoints <= 0) strengthPoints = 0; }
private: void mob(int strengthReduced) { int total = strengthReduced/4; defensePoints.num -= total; if(defensePoints.num <=0) defensePoints.num = 0;
cout<<"New DefensePoints for BlueMen :"<
} };
class Medusa : public Creature { public : Medusa() { cout<<"Medusa Selected"< attackPoints.num = 2; attackPoints.sides = 6; defensePoints.num = 1; defensePoints.sides = 6; armorPoints = 3; strengthPoints = 8; }
int attack() { int res = generateRollDice(attackPoints.num, attackPoints.sides); cout<<"Attack Points generated :"+res< if(res == 12) { cout<<"Medusa used Glare."< } return res; }
void defense(int points) { int defPoints = generateRollDice(defensePoints.num, defensePoints.sides); cout<<"Defense points Generated : "+defPoints< int finalDefensePoints = points - defPoints - armorPoints; if(finalDefensePoints <= 0) { finalDefensePoints = 0; cout<<"Defense is stronger."; } strengthPoints -= finalDefensePoints; if(strengthPoints <= 0) strengthPoints = 0; } };
class HarryPotter : public Creature { public : HarryPotter() { cout<<"HarryPotter Selected"< attackPoints.num = 2; attackPoints.sides = 6; defensePoints.num = 2; defensePoints.sides = 6; armorPoints = 0; strengthPoints = 10; reBorn =0; }
int attack() { int res = generateRollDice(attackPoints.num, attackPoints.sides); cout<<"Attack points Generated :"+res< return res; }
void defense(int points) { int defPoints = generateRollDice(defensePoints.num, defensePoints.sides); cout<<"Defense points Generated : "+defPoints< int finalDefensePoints = points - defPoints - armorPoints; if(finalDefensePoints <= 0) { finalDefensePoints = 0; cout<<"Defense is stronger."; } strengthPoints -= finalDefensePoints; if(strengthPoints <= 0) { strengthPoints = 0; Hogwarts(); } }
private : int reBorn;
//reBorn with 20 points if first time death. void Hogwarts () { if(reBorn == 0) { reBorn = 1; strengthPoints = 20; } }
};
int main() {
cout<<"Select two Creatures to fight."< cout<<"1. Vampire"< cout<<"2. Barbarian"< cout<<"3. Blue Mean"< cout<<"4. Medusa"< cout<<"5. Harry Potter"<
//Selecting creatures
Vampire v = Vampire(); HarryPotter hp = HarryPotter();
while(v.strengthPoints >0 && hp.strengthPoints >0) { int points = v.attack(); hp.defense(points);
if(hp.strengthPoints == 0) { cout<<"Harry Potter Dies. Vampire Wins."< } else { points = hp.attack(); v.defense(points); }
if(v.strengthPoints == 0) cout<<"Vampire Dies."< }
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
