Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is C++ I need help fixing the errors please help thank you #include #include #include #include #define MAX 100 using namespace std; class Weapon{

This is C++

I need help fixing the errors please help thank you

#include

#include

#include

#include

#define MAX 100

using namespace std;

class Weapon{

private:

string name;

int strength;

public:

Weapon(){}

Weapon(string name, int stregnth){

this->name = name;

this->strength = strength;

}

void setName(string name){

this->name = name;

}

void setStrength(int strength){

this->strength = strength;

}

string getName(){

return name;

}

int getStrength(){

return strength;

}

};

class Warrior{

private:

string name;

Weapon weapon;

public:

Warrior(){}

Warrior(string name, Weapon weapon){

this->name = name;

this->weapon = weapon;

}

void setName(string name){

this->name = name;

}

void setWeapon(Weapon weapon){

this->weapon = weapon;

}

string getName(){

return name;

}

Weapon& getWeapon(){

return weapon;

}

bool isDead(){

if(weapon.getStrength() <= 0) return true;

else return false;

}

void battle(Warrior& W){

cout << name << " battles " << W.getName() << endl;

int sone;

int stwo;

if(isDead() == true && W.isDead() == true){

cout << "Oh, NO! They're both dead! Yuck!" << endl;

}

else if(isDead() == false && W.isDead() == true){

cout << "He's dead, " << name << endl;

}

else if(isDead() == true && W.isDead() == false){

cout << "He's dead, " << W.getName() << endl;

}

else if(weapon.getStrength() == W.getWeapon().getStrength()){

cout << name << " defeats " << W.getName() << endl;

weapon.setStrength(weapon.getStrength() - W.getWeapon().getStrength());

W.getWeapon().setStrength(0);

}

else if(weapon.getStrength() < W.getWeapon().getStrength()){

cout << W.getName() << " defeats " << name << endl;

weapon.setStrength(0);

W.getWeapon().setStrength(W.getWeapon().getStrength() - weapon.getStrength());

}

}

};

class BattleField{

private:

Warrior W[MAX];

int count;

public:

BattleField(){

count = 0;

}

void insertWarrior(Warrior wr){

if(count < MAX){

W[count] = wr;

count++;

}

}

int getWarrior(string name){

for(int i = 0; i < count; i++){

if(W[i].getName().compare(name) == 0) return i;

}

}

void display(){

cout << "There are: " << count << " warriors" << endl;

for(int i = 0; i < count; i++){

cout << "Warrior: " << W[i].getName() << ", weapon: " << W[i].getWeapon().getName()

<< ", " << W[i].getWeapon().getStrength() << endl << endl;

}

}

void readWarrior(fstream& file){

string str;

string weaponName;

string warriorName;

int strength;

int status = 0;

while(file >> str){

if(str.compare("Warrior") == 0){

file >> warriorName;

file >> weaponName;

file >> str;

istringstream(str) >> strength;

Weapon wp(weaponName, strength);

Warrior wr(warriorName, wp);

insertWarrior(wr);

}

else if(str.compare("Status") == 0){

status++;

if(status == 1) display();

if(status == 2) break;

}

else if(str.compare("Battle") == 0){

string name1;

string name2;

file >> name1;

file >> name2;

W[getWarrior(name1)].battle(W[getWarrior(name2)]);

}

}

display();

}

};

int main(int argc, const char * argv[]) {

fstream file;

file.open("warriors.txt");

BattleField BF;

BF.readWarrior(file);

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Seven Databases In Seven Weeks A Guide To Modern Databases And The NoSQL Movement

Authors: Luc Perkins, Eric Redmond, Jim Wilson

2nd Edition

1680502530, 978-1680502534

More Books

Students also viewed these Databases questions