Question: My warriors.txt file will not open it keeps just saying error , this is my code #include #include #include #include using namespace std; struct Weapon
My warriors.txt file will not open it keeps just saying error this is my code
#include
#include
#include
#include
using namespace std;
struct Weapon
private:
string name;
Our constructor weapon
public:
Weaponconst string &name : namename
Here are my getters
string getname const return name;
I am also adding some setters
void setnameconst string &newNamename newName;
since we put vaules in weapons and made it private we must use a friend fucntion
Friendfucntion
friend ostream &operator ostream &os const Weapon& sword;
;
struct Warriors
private:
string name;
int strength;
Weapon sword;
public:
we use a construct here
Warriorsconst string& Warriorname, int Warriorstrength, const Weapon& sword
: nameWarriorname strengthWarriorstrength swordsword
my getters
string getname const return name;
int getstrength const return strength;
Weapon getsword const return sword;
my setters
void setnameconst string &newNamename newName;
void setstrengthint newStrengthstrength newStrength;
void setswordconst Weapon&newSwordsword newSword;
friend ostream &operator ostream &os const Warriors& warrior;
;
Here are my function prototypes
void openfileifstream& warriorfile;
void Warriorcommandvector& warriors, const string& Warriorname, int Warriorstrength, const Weapon& sword;
void battlecommandvector& warriors, const string& Warriorname const string& Warriorname;
void statusupdateconst vector& warriors;
my main function
int main
ifstream warriorfile;
openfilewarriorfile;
string command;
vector warriors;
Setting up my command for to game
while warriorfile command
if command "Warrior"
string name, weaponName;
int strength, weaponStrength;
warriorfile name strength weaponName weaponStrength;
Weapon swordweaponName; This will create a Weapon object
Warriorcommand warriors, name, strength, sword;
else if command "Battle"
string Warriorname Warriorname;
warriorfile Warriorname Warriorname;
battlecommandwarriors Warriorname Warriorname;
else if command "Status"
statusupdatewarriors;
return ;
warriorfile.close;
void openfileifstream& warriorfile
warriorfile.clear;
warriorfile.openwarriorstxt;
ifwarriorfile
cerr "Error opening warrior.txt endl;
return;
warriorfile.close;
void Warriorcommandvector &warriors, const string &Warriorname, int Warriorstrength, const Weapon &sword
forsizet i ; i warriors.size; i
ifwarriorsigetname Warriorname
cerr error" Warriorname endl;
return;
warriors.emplacebackWarriorname, Warriorstrength, sword;
void battlecommandvector& warriors, const string& Warriorname const string& Warriorname
int index;
int index;
forint i ; i warriors.size; i
ifwarriorsigetname Warriorname
index i;
ifwarriorsigetname Warriorname
index i;
if index
cerr "Battle command not found" endl;
if index
cerr "Battle command not found" endl;
return;
Warriors &warrior warriorsindex;
Warriors &warrior warriorsindex;
cout warriorgetname warriorgetname endl;
int strength warriorgetstrength;
int strength warriorgetstrength;
ifstrength && strength
cout "Both warriors have died" endl;
else if strength
cout "This warrior right here is dead dude" & warriors
cout "Warrior status update" warriors.size "warriors" endl;
for const Warriors& warrior : warriors
cout warrior.getname warrior.getstrength endl;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
