+ 2
Solved: C++ Characters Fully Restoring Health Between Attacks
Hey guys, I would really appreciate help here. I'm trying to improve my skills on classes, inheritance, etc. by creating an adventure game where a player fights monsters. (pretty standard way to practice C++) and for some reason my characters are fully restoring their health between turns, meaning that the while loop I created to only break when someone's health drops to or below 0 is never-ending so the game times out. Could someone look at the code I shared and help me figure out why their health is restoring? Thank you guys so much in advance! UPDATE: Solved (thanks so much guys) https://code.sololearn.com/c09QaKxED7w9
2 Respuestas
+ 3
In your function fightMonster and fightPlayer you're setting the health of monster and player resp. at the end. But since the argument to these functions are characters that are "passed by value". This wouldn't affect the health of monster or player overall after the function exit.
You need to pass "monster in fightMonster and player in fightPlayer by "pass_by_reference" using "&" in those function arguments
ie. In line 81
void fightMonster(Character player, Character &monster){
...
and in line 108
void fightPlayer(Character &player, Character monster){
...
+ 1
Thank you so much! I completely forgot about that! Code is running again!