C++ This program brings up the menu properly but exits immediately. I would like
ID: 3838912 • Letter: C
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 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;
}
Explanation / Answer
replace main with below:
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
int firstplayer;
int seondplayer;
cout << "Please select two players saparated by space and then press Enter:" << endl;
cin >> firstplayer;
cin >> secondplayer;
if((firstplayer == 1 && secondplayer == 5)||(firstplayer == 5 && secondplayer == 1)){
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. Harry Potter Wins"<
}
}else if((firstplayer == 1 && secondplayer == 2)||(firstplayer == 2 && secondplayer == 1)){
Vampire v = Vampire();
Barbarian b = Barbarian ();
while(v.strengthPoints >0 || b.strengthPoints >0) {
int points = v.attack();
b.defense(points);
if(b.strengthPoints == 0) {
cout<<"Barbarian Dies. Vampire Wins."<
} else {
points = b.attack();
v.defense(points);
}
if(v.strengthPoints == 0)
cout<<"Vampire Dies. Barbarian Wins"<
}
}
/*
Repeat for all
players: 1, 2, 3, 4, 5.
i completed 1:2(Vampire vs Barbarian) , 1:5(Vampire vs HarryPotter)
you have complete others
for: 1:3, 1:4, 2:3, 2:4, 2:5, 3:4, 3:5, 4:5
again use || in while loop
hope you understand.
*/
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.