Pls help me. I'm struggling with how to implement the change in health into the respective Monster and Hero. I'm only supposed to modify character.cpp. get_name is supposed to get the character's name while get_health and set_health are supposed to get the health of the character, but how do I implement the difference between the Hero and Monster and the changes to their health in these variables?
main.cpp:
#include "Character.h"
usingnamespace std;
int main() {
int monster_health = 10;
int monster_damage = 3;
srand(time(0));
//This is the character you play
Character C("Hero");
// This is the monster you try to defeat, the monster has 0 arrow
Character M("Monster", monster_health, monster_damage, 0);
while (M.get_health() > 0 && C.get_health() > 0)
{
M.attack(C);
cout << "----------------------\n";
if (C.get_health() <= 0)
break;
C.display();
cout << "What do you do? 1 attack, 2 fire arrow, Q exit: ";
int choice;
cin >> choice;
if (cin.fail())
break;
switch (choice)
{
case 1:
C.attack(M);
break;
case 2:
C.rangedAttack(M);
break;
}
}
if (M.get_health() <= 0)
cout << "Congratulations! You killed the monster!" << endl;
if (C.get_health() <= 0)
cout << "You have died! GAME OVER! " << endl;
cout << "Thanks for playing!" << endl;
return 0;
}
Hi thanks for the reply. This is my character.cpp now. A lot of things work, but a few are not. If I use the ranged attack first, the regular attack for the hero also becomes the randomized ranged attack (even though the damage for the hero is set at 2). How do I fix this without declaring a new variable within rangedAttack?
My second issue is that my debugger just closes when either the monster or the hero health goes below 0. The -health isn't printed and final cout statements are not printed by the main.
How do I fix this without declaring a new variable within rangedAttack?
What's wrong with declaring a new variable?
Presumable your damage data member is intended for melee damage, so you shouldn't be using it at all here. Just declare a local variable for the ranged damage here.
If you're really desperate to avoid declaring a local variable, you could use the expression for calculating it directly in the call to set_health(), but I don't see much point. Trying to cram as much as possible into a single line just makes your code harder to read.
My second issue is that my debugger just closes when either the monster or the hero health goes below 0. The -health isn't printed and final cout statements are not printed by the main.
Presumably this is just your program ending naturally, and your debugger closing the shell window that it's running the program in?