I am making a simple game for learning purposes mostly and I recently ran into this problem. Keep in mind that I'm still a huge beginner. When I go into the game from the menu and write anything in the "Command Line" I instantly starve and dehydrate. I haven't been able to connect to the internet for a couple of days and I've read through the entire program but I can't find anything wrong.
menu.h
#include <iostream>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <dos.h>
#include <windows.h>
#include <WinBase.h>
//-------------//
#include "tutorial.h"
#include "game.h"
void menu() {
std::cout << "-------MENU------- \n";
std::cout << " 1.Play \n";
std::cout << " 2.Tutorial \n";
std::cout << " 3.Exit \n";
std::cout << " \n";
std::cout << " \n";
std::cout << " \n";
std::cout << "Choose Option: ";
int menuOption;
std::cin >> menuOption;
int menuLoop = 0;
while (menuLoop != 1) {
if (menuOption == 1) {
menuLoop = 1;
play();
}
if (menuOption == 2) {
menuLoop = 1;
system("CLS");
tutorial();
}
if (menuOption == 3) {
menuLoop = 1;
std::cout << "Bye!";
Sleep(1000);
}
if (menuOption > 3)
std::cout << "\"" << menuOption << "\"" << " is not a valid option.\n";
}
}
game.h
#include <iostream>
#include <string>
#include <windows.h>
#include <WinBase.h>
//initiating functions
void step();
void run();
void theme();
void starve();
void die();
void dehydrate();
void b();
//globals
std::string name;
std::string commandLine;
int onRoad = 1; // 1 = True, 0 = False
int steps = 0;
double hunger = 0.0;
double thirst = 0.0;
int energy = 5;
void play() {
system("CLS");
std::cout << "Enter your name: \n";
std::cin >> name;
system("CLS");
theme();
Sleep(350);
std::cout << " " << name << "'s Roadtrip\n";
std::cout << "Type \"/help\" for help\n";
std::cout << "---------Command Line---------\n";
std::cin >> commandLine;
while (onRoad != 0){
//------------------Conditions start------------------
// Hunger Conditions
if (hunger = 0){
if (hunger < 0){
std::cout << "You can't eat that, you're not hungry.\n";
b();
}
}
if (hunger > 100){
hunger = 100;
}
if (hunger < 0){
hunger = 0;
}
if (hunger = 100){
starve();
}
else if (hunger > 96){
std::cout << "You're extremely hungry! If you don't eat something quick you're going to die!\n";
b();
}
else if (hunger > 90) {
std::cout << "You're very hungry.\n";
b();
}
else if (hunger > 80) {
std::cout << "You're hungry.\n";
b();
}
// Thirst Conditions
if (thirst = 0){
if (thirst < 0){
std::cout << "You can't drink that, you're not thirsty.\n";
}
}
if (thirst < 0){
thirst = 0;
}
if (thirst > 100) {
thirst = 100;
}
if (thirst = 100){
dehydrate();
}
else if (thirst > 90){
std::cout << "You're extremely thirsty! If you don't drink something quick you're going to die!\n";
b();
}
else if (thirst > 75) {
std::cout << "You're very thirsty.\n";
b();
}
else if (thirst > 50){
std::cout << "You're thirsty.\n";
b();
}
//Energy Conditions
if (energy > 10){
energy = 10;
}
if (energy < 0){
energy = 0;
}
//-------------------Conditions end-------------------
if (commandLine == "/commands"){
std::cout << "-Command- -Action-\n";
std::cout << " /help Displays this menu.\n";
std::cout << " /commands Displays list of commands.\n";
std::cout << " /step Take a step and display total amount of steps.\n";
std::cout << " /run Take 5 steps and consume 5 energy.\n";
std::cout << " Doesn't increase hunger or thirst.\n";
std::cout << " /inventory Displays inventory.\n";
std::cout << " /info Displays stats.\n";
b();
}
if (commandLine == "/step") {
step();
b();
}
if (commandLine == "/info") {
std::cout << name << "'s stats\n";
std::cout << "Hunger: " << hunger << std::endl;
std::cout << "Thirst: " << thirst << std::endl;
std::cout << "Energy: " << energy << std::endl;
b();
}
else {
std::cout << commandLine << " is not a valid command. Type /commands to display commands.\n";
b();
}
}
}
void step(){
steps += 1;
std::cout << steps;
hunger += 5;
thirst += 5;
}
void run() {
steps += 5;
std::cout << steps;
}
void starve(){
std::cout << "You starved to death!\n";
die();
}
void dehydrate(){
std::cout << "You dehydrated!\n";
die();
}
void die(){
std::cout << "Steps taken: " << steps << std::endl;
onRoad = 0;
}
void theme(){
Beep(600, 200);
Beep(500, 200);
Beep(800, 400);
}
// b takes you back to the command line
void b(){
std::cin >> commandLine;
}
main.cpp
#include <iostream>
#include "menu.h"
#include <WinBase.h>
#include <windows.h>
int main(){
menu();
system("PAUSE");
return 0;
}
**EDIT: ** Pic: http://i.imgur.com/yu1V1pq.png (need 10 rep to post picture)
This is really weird. I entered /step and it worked, and then i entered /run and it also worked. I don't understand...
Some of your if statements do assignment instead of comparison
if (hunger = 100){
starve();
}
You probably need to change = to ==
Enable warnings while compiling, if you have not already done so.
Because
// b takes you back to the command line
void b(){
std::cin >> commandLine;
}
b doesn't take you back to the command line just wait for a character to be read and then it returns. If you want to go back, you should follow the way you came from. For example exiting play will return you to the menu loop, obviously with menuLoop = 1 so it will exit the whole program but with modifications this is not a bad looping system.
Edit: I've seen what you do mean in the "command line".
Like others said, you have a load of conditions accidentally spelled as assignments.
Also, indeed, the b() function is eating subsequent commands.
Maybe you should
use std::getline() to read a command one line at a time
or use std::cin.ignore() inside b() to actually consume until the end of the line
PS. Due to the use of globals I have a hard time verifying the game loop logic. I just know that /step after /step gets ignored without effect right now. Separate your input from the loop control and try to remove the global variables.
INFO
Instead of writing std::cout every single time you can just write using namespace std; on the beginning after that you dont need to write std::cout just write cout << "" ;
Related
Working on an assignment to have a duel play out amongst three players with varying accuracy and needs them to shoot in order. Aaron has an accuracy of 1/3, Bob has an accuracy of 1/2, and Charlie never misses. A duel should loop until one is left standing.
Here is my code so far and it only ever causes the first two players to miss and Charlie wins even when the random number generator should constitute a hit.
#include <iostream>
#include <ctime>
#include <cstdlib>
using std::cin;
using std::cout;
using std::endl;
void shoot(bool & targetAlive, double accuracy);
int startDuel();
int main()
{
srand(time(NULL));
startDuel();
return 0;
}
void shoot(bool &targetAlive, double accuracy)
{
double x;
x = (((float)rand()/(float)(RAND_MAX))*1.0);
if (x < accuracy)
{
cout << "target is hit!" << endl;
targetAlive = false;
}
else
cout << "missed!" << endl;
cout << x << endl;
targetAlive = true;
}
int startDuel()
{
int a = 0, b = 0, c = 0;
bool aaronAlive, bobAlive, charlieAlive;
shoot(charlieAlive, 1.0/3);
if (charlieAlive)
{
cout << "Aaron missed Charlie!" << endl;
shoot(charlieAlive, 0.5);
if (charlieAlive)
{
cout << "Bob missed Charlie so Charlie throws back!" << endl;
cout << "Bob has been hit by Charlie!" << endl;
bobAlive = false;
shoot (charlieAlive, 1.0/3);
if (charlieAlive)
{
cout << "Aaron missed Charlie, so Charlie throws back!" << endl;
aaronAlive = false;
cout << "The duel is over and Charlie wiped them all out" << endl;
return (c++);
}
}
}
else if (!charlieAlive)
{
cout << "Aaron hit Charlie" << endl;
do
{
shoot(aaronAlive, 0.5);
shoot(bobAlive, 1.0/3);
if (!aaronAlive)
{
cout << "Bob won!" << endl;
return (b++);
}
else if (!bobAlive)
{
cout << "Aaron won!" << endl;
return (a++);
}
}
while((aaronAlive)&&(bobAlive));
}
}
look at the last line of shoot
void shoot(bool& targetAlive, double accuracy)
{
double x;
x = (((float)rand() / (float)(RAND_MAX)) * 1.0);
if (x < accuracy)
{
cout << "target is hit!" << endl;
targetAlive = false;
}
else
cout << "missed!" << endl;
cout << x << endl;
targetAlive = true; <<<<====
}
no matter what happens before you reset alive to true before exiting
just remove that line
also move srand to main, you should only call it once.
I have a board game that has spaces (has numeral values 1,2,3, etc.) starting from 1 and 16 pieces of pawns; four for each player.
I want to show the result of my board game at some point. I tried the method below but that will make my code extremely long.
i have 16 pieces and 100 spaces that i have to repeat that code with 100 space that would take forever. the code below is just for one space (the first space)
Any idea how to show my result in a short way? Thanks in advance!
Here is my old-fashioned way:
//space 1
if (bpiece1->value == 1)
{
cout << " bpiece1";
}
else if (bpiece2->value == 1)
{
cout << " bpiece2";
}
else if (bpiece3->value == 1)
{
cout << " bpiece3";
}
else if (bpiece4->value == 1)
{
cout << " bpiece4";
}
else if (gpiece1->value == 1)
{
cout << " gpiece1";
}
else if (gpiece2->value == 1)
{
cout << " gpiece2";
}
else if (gpiece3->value == 1)
{
cout << " gpiece3";
}
else if (gpiece4->value == 1)
{
cout << " gpiece4";
}
else if (ypiece1->value == 1)
{
cout << " ypiece1";
}
else if (ypiece2->value == 1)
{
cout << " ypiece2";
}
else if (ypiece3->value == 1)
{
cout << " ypiece3";
}
else if (y4->value == 1)
{
cout << " y4";
}
else if (rpiece1->value == 1)
{
cout << " rpiece1";
}
else if (rpiece2->value == 1)
{
cout << " rpiece2";
}
else if (rpiece3->value == 1)
{
cout << " rpiece3";
}
else if (rpiece4->value == 1)
{
cout << " rpiece4";
}
else
{
cout << " 01";
}
C++ is an object-oriented language. Therefore, we start by creating a class that stores your board and implements all functions on it. Like
//Board.h
#include <array>
using std::array;
enum class Figure { None, Pawn };
class Board {
private:
array<array<Figure, 8>, 8> fields; //8x8 if it was a chess board
public:
void print() const;
};
//Board.cpp
#include "Board.h"
#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;
inline string to_string(const Figure figure){
switch(figure){
case Figure::None:
return " ";
case Figure::Pawn:
return "p";
}
//throw error here
return "";
}
void Board::print() const {
for(size_t i = 0; i < fields.size(); i++){
for(size_t j = 0; j < fields[i].size(); j++){
cout << to_string(fields[i][j]);
}
cout << endl;
}
cout << endl;
}
If this is new to you, you should really read the basic tutorials first and make sure that you understand each line I wrote in the end.
Important here is: Representation, represenation, representation. Don't think in "1 is a pawn", think in "a pawn is a pawn". Everything that has a function which you can think of should probably be a class, a structure or an enum.
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
void armySkirmish();
void battleOutcome();
string commander = "";
int numberOfHumans = 0;
int numberOfZombies = 0;
class ArmyValues
{
protected:
double attackPower;
double defensePower;
double healthPoints;
public:
void setAttackPower(double a)
{
attackPower = a;
}
void setDefensePower(double d)
{
defensePower = d;
}
void setHealthPoints(double h)
{
healthPoints = h * (defensePower * .1);
}
};
class Zombies: public ArmyValues
{
};
class Humans: public ArmyValues
{
};
int main(int argc, char ** argv)
{
cout << "Input Commander's Name: " << endl;
cin >> commander;
cout << "Enter Number of Human Warriors: " << endl;
cin >> numberOfHumans;
cout << "Enter Number of Zombie Warriors: " << endl;
cin >> numberOfZombies;
armySkirmish();
battleOutcome();
return 0;
}
void armySkirmish()
{
cout << "\nThe Humans tense as the sound of the undead shuffle towards them." << endl;
cout << commander << " shuffles forward with a determined look." << endl;
cout << "The undead form up into ranks and growl a war chant!" << endl;
cout << commander <<" shouts, CHARGE!!!" << endl;
cout << endl;
cout << "Warriors from both sides blitz across the field!" << endl;
cout << endl;
cout << "*The Carnage has begun!*" << endl;
cout << "*Steal, Sparks, and Flesh flies" << endl;
}
void battleOutcome()
{
int zombieLives = numberOfZombies;
int humanLives = numberOfHumans;
int randomNumber = 0;
int humanDeath = 0;
int zombieDeath = 0;
double newHumanLife = 0;
double newZombieLife = 0;
Zombies zombieBattleData;
Humans humanBattleData;
srand(time(NULL));
zombieBattleData.setAttackPower(20.0);
humanBattleData.setAttackPower(35.0);
zombieBattleData.setDefensePower(15.0);
humanBattleData.setDefensePower(20.0);
zombieBattleData.setHealthPoints(150.0);
humanBattleData.setHealthPoints(300.0);
while(zombieLives && humanLives > 0)
{
randomNumber = 1+(rand()%10);
if(randomNumber < 6)
{
newHumanLife = humanBattleData.healthPoints - zombieBattleData.attackPower;
if(newHumanLife <= 0)
{
humanLives--;
humanDeath++;
}
}else
{
newZombieLife = zombieBattleData.healthPoints - humanBattleData.attackPower;
if(newZombieLife <= 0)
{
zombieLives--;
zombieDeath++;
}
}
}
if(zombieLives <= 0)
{
cout << "Humans have emerged victorious!" << endl;
cout << "Human Deaths: " << humanDeath << "Zombie Deaths: " << zombieDeath << endl;
}else if(humanLives <= 0)
{
cout << "Zombies have emerges victorious!" << endl;
cout << "Human Deaths: " << humanDeath << "Zombie Deaths: " << zombieDeath << endl;
}
I know the code wont run properly as of now. What I was doing was a test run to make sure I was receiving no errors. The two errors I'm getting are:
armySimulatorMain.cpp:25:10: error: 'double ArmyValues::healthPoints' is protected
armySimulatorMain.cpp:115:67: error: within this context.
newHumanLife = humanBattleData.healthPoints - zombieBattleData.attackPower;
This is the case for Attack Power and Health Power however, Defense power is clearing the errors. i don't understand why they are getting flagged. I'm changing the variable through the public function so shouldn't this be allowed?
Also, I'm calling three variables outside of all functions because they are being used by multiple functions. How can I plug those variables somewhere I don't like that they are floating freely above everything?
Thanks guys I can't believe I forgot about getters... Anyway the code runs now much appreciated I'll make sure to remember this time xD
It's not complaining about the line where you set the values; as you say, that uses a public function. But here, you try to read the protected member variables:
newHumanLife = humanBattleData.healthPoints - zombieBattleData.attackPower;
You only try to read two variables, and those are the ones it complains about.
You'll need a public getter function to read the values.
You need to do something like:
public:
double gethealthPoints()
{
return healthPoints;
}
because attackPower, defensePower, healthPoints are all protected, so if you want to access to any of them you need a getter, otherwise you will always receive an protect error
I am trying to access a member of one class in another class. I am fairly new to C++ so forgive me if this is an easy fix but I cannot find the answer, so I came here.
In this instance I would like to call "init();" from class CGuessNumber and member CheckNumber.
Here is my code.
#include <iostream>
#include <ctime>
#include <cstdlib>
class CGuessNumber
{
public:
int GenerateNumber()
{
return rand() % 100 + 1;
}
void checkNumber(int guess, int answer, int &attempts)
{
if (guess < answer)
{
std::cout << "TOO LOW, TRY AGAIN" << "\n" << "TRYS LEFT: " << attempts << "\n";
attempts--;
}else if(guess > answer)
{
std::cout << "TOO HIGH, TRY AGAIN" << "\n" << "TRYS LEFT: " << attempts << "\n";
attempts--;
}else if(guess == answer)
{
std::cout << "YOU WON!" << "\n" << "TRYS LEFT: " << attempts << "\n";
}
if (attempts <= 0)
{
std::cout << "YOU LOST!" << "\n" << "TRYS LEFT: " << attempts << "\n";
CGAME::init(answer, attempts);
}
}
}Number;
class CGAME
{
public:
void init(int &answer, int &attempts)
{
answer = Number.GenerateNumber();
attempts = 5;
};
int newGame()
{
srand (time(NULL));
int intAnswer, playerGuess, trys;
init(intAnswer, trys);
while(intAnswer != playerGuess and trys > 0)
{
std::cin >> playerGuess;
Number.checkNumber(playerGuess, intAnswer, trys);
}
};
}ONewGame;
int main()
{
CGAME ONewGame
ONewGame.newGame();
return 0;
}
I think, this is what you're looking for
Basically you can pass a pointer which points to one object into a constructor of the other. In this case we just pass a pointer to CGuessNumber into the CGAME constructor, we also store this pointer in a private field so we can use it. Then you can use this pointer to call methods.
working example (pointer->method syntax)
working example (reference.method syntax)
#include <iostream>
#include <ctime>
#include <cstdlib>
class CGuessNumber
{
public:
int GenerateNumber()
{
return rand() % 100 + 1;
}
void checkNumber(int guess, int answer, int &attempts)
{
if (guess < answer)
{
std::cout << "TOO LOW, TRY AGAIN" << "\n" << "TRYS LEFT: " << attempts << "\n";
attempts--;
}else if(guess > answer)
{
std::cout << "TOO HIGH, TRY AGAIN" << "\n" << "TRYS LEFT: " << attempts << "\n";
attempts--;
}else if(guess == answer)
{
std::cout << "YOU WON!" << "\n" << "TRYS LEFT: " << attempts << "\n";
}
if (attempts <= 0)
{
std::cout << "YOU LOST!" << "\n" << "TRYS LEFT: " << attempts << "\n";
}
}
};
class CGAME
{
public:
CGAME(CGuessNumber* pNumber)
{
m_number = pNumber;
}
void init(int &answer, int &attempts)
{
answer = m_number->GenerateNumber();
attempts = 5;
};
void newGame()
{
srand (time(NULL));
int intAnswer, playerGuess, trys;
init(intAnswer, trys);
while(intAnswer != playerGuess and trys > 0)
{
std::cin >> playerGuess;
m_number->checkNumber(playerGuess, intAnswer, trys);
}
};
private:
CGuessNumber* m_number;
};
int main()
{
CGuessNumber* pGnum = new CGuessNumber();
CGAME* ONewGame = new CGAME(pGnum);
ONewGame->newGame();
return 0;
}
Let me just address the syntax errors.
In the checkNumber() function:
...
CGAME::init(answer, attempts);
...
There are 2 problems with this:
CGAME is not declared yet, so the compiler doesn't know it exists, or what it is. To avoid this, usually all the classes are declared at the top (or in a header file) and all there functions are defined later.
You can't call a member function of a class without an object, unless it's a static function. This function can be static as it doesn't use member variables (there are design issues, but lets ignore them for now).
Also in main() you missed a ';', but I think you already know that :-)
So, applying these changes:
#include <iostream>
#include <ctime>
#include <cstdlib>
// only declaring the classes here
class CGAME
{
public:
static void init(int &answer, int &attempts);
int newGame();
}ONewGame;
class CGuessNumber
{
public:
int GenerateNumber();
void checkNumber(int guess, int answer, int &attempts);
}Number;
// defining all the class member functions now
int CGAME::newGame()
{
srand (time(NULL));
int intAnswer, playerGuess, trys;
init(intAnswer, trys);
while(intAnswer != playerGuess and trys > 0)
{
std::cin >> playerGuess;
Number.checkNumber(playerGuess, intAnswer, trys);
}
}
int CGuessNumber::GenerateNumber()
{
return rand() % 100 + 1;
}
void CGuessNumber::checkNumber(int guess, int answer, int &attempts)
{
if (guess < answer)
{
std::cout << "TOO LOW, TRY AGAIN" << "\n" << "TRYS LEFT: " << attempts << "\n";
attempts--;
}else if(guess > answer)
{
std::cout << "TOO HIGH, TRY AGAIN" << "\n" << "TRYS LEFT: " << attempts << "\n";
attempts--;
}else if(guess == answer)
{
std::cout << "YOU WON!" << "\n" << "TRYS LEFT: " << attempts << "\n";
}
if (attempts <= 0)
{
std::cout << "YOU LOST!" << "\n" << "TRYS LEFT: " << attempts << "\n";
CGAME::init(answer, attempts);
}
}
void CGAME::init(int &answer, int &attempts)
{
answer = Number.GenerateNumber();
attempts = 5;
}
int main()
{
CGAME ONewGame;
ONewGame.newGame();
return 0;
}
I'm following a tutorial for making a MUD (text-based RPG), and I am having issues with my main function. If you'll look at the code, you'll see that when the player moves it will check for a random encounter, and if monster != 0, it will go into the combat loop. When I execute this in the command prompt, it will allow me to attack the monster, but it never makes it to the monster->attack(mainPlayer) function. It just goes back to the screen that states whether I want to move, rest, view stats, or quit. Any help with this would be greatly appreciated!
#include "stdafx.h"
#include "Map.h"
#include "Player.h"
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
int main()
{
srand( time(0) );
Map gameMap;
Player mainPlayer;
mainPlayer.createClass();
// Begin adventure
bool done = false;
while( !done )
{
// Each loop cycle we output the player position and
// a selection menu.
gameMap.printPlayerPos();
int selection = 1;
cout << "1) Move 2) Rest 3) View Stats 4) Quit: ";
cin >> selection;
Monster* monster = 0;
switch( selection )
{
case 1:
// Move the player
gameMap.movePlayer();
// Check for a random encounter. This function
// returns a null pointer if no monsters are
// encountered.
monster = gameMap.checkRandomEncounter();
// 'monster' not null, run combat simulation.
if( monster != 0)
{
// Loop until 'break' statement.
while( true )
{
// Display hitpoints
mainPlayer.displayHitPoints();
monster->displayHitPoints();
cout << endl;
// Player's turn to attack first.
bool runAway = mainPlayer.attack(*monster);
if( runAway )
{
break;
}
if( monster->isDead() )
{
mainPlayer.victory(monster->getXPReward());
mainPlayer.levelUp();
break;
}
monster->attack(mainPlayer);
if( mainPlayer.isDead() )
{
mainPlayer.gameover();
done = true;
break;
}
}
// The pointer to a monster returned from
// checkRandomEncounter was allocated with
// 'new', so we must delete it to avoid
// memeory leaks.
delete monster;
monster = 0;
}
break;
case 2:
mainPlayer.rest();
break;
case 3:
mainPlayer.viewStats();
break;
case 4:
done = true;
break;
} // End switch statement
} // End While statement
} // End main function
Here is the Player::attack function:
bool Player::attack(Monster& monster)
{
int selection = 1;
std::cout << "1) Attack 2) Run: ";
std::cin >> selection;
std::cout << std::endl;
switch( selection )
{
case 1:
std::cout << "You attack the " << monster.getName()
<< " with a " << mWeapon.mName << std::endl;
if( Random(0, 20) < mAccuracy )
{
int damage = Random(mWeapon.mDamageRange);
int totalDamage = damage - monster.getArmor();
if( totalDamage <= 0)
{
std::cout << "Your attack failed to penetrate the "
<< monster.getName() << "'s armor." << std::endl;
}
else
{
std::cout << "You attack for " << totalDamage
<< " damage!" << std::endl;
// Subtract from monster's hitpoints.
monster.takeDamage(totalDamage);
}
}
else
{
std::cout << "You miss!" << std::endl;
}
std::cout << std::endl;
break;
case 2:
// 25% chance of being able to run.
int roll = Random(1, 4);
if( roll == 1 )
{
std::cout << "You run away!" << std::endl;
return true; //<-- Return out of the function.
}
else
{
std::cout << "You could not escape!" << std::endl;
break;
}
}
}
And here is the Monster::attack function:
void Monster::attack(Player& player)
{
cout << "A " <<mName << " attacks you "
<< "with a " << mWeapon.mName << std::endl;
if( Random(0,20) < mAccuracy )
{
int damage = Random(mWeapon.mDamageRange);
int totalDamage = damage - player.getArmor();
if( totalDamage <= 0 )
{
cout << "The " << mName << "'s attack failed to "
<< "penetrate your armor." << endl;
}
else
{
cout << "You are hit for " << totalDamage
<< " damage!" << endl;
player.takeDamage(totalDamage);
}
}
else
{
cout << "The " << mName << " missed!" << endl;
}
cout << endl;
}
Your Player::attack() method has only one return-statement: return true;. You forgot to add the final line return false; to your method.
This could have easily been prevented if you enable warnings (and pay attention to them!)
Your Player::attack doesn't return in all cases (specifically when it needs to return false). When the calling function tries to access the return value of Player::Attack it will get junk and so you enter the if(ranAway) block and break out of your while loop