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:
Weapon(const string &name) : name(name){}
// Here are my getters
string get_name() const {return name;}
// I am also adding some setters
void set_name(const string &newName){name = 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
Warriors(const string& Warrior_name, int Warrior_strength, const Weapon& sword )
: name(Warrior_name), strength(Warrior_strength), sword(sword){}
// my getters
string get_name() const {return name;}
int get_strength() const {return strength;}
Weapon get_sword() const {return sword;}
// my setters
void set_name(const string &newName){name = newName;}
void set_strength(int newStrength){strength = newStrength;}
void set_sword(const Weapon&newSword){sword = newSword;}
friend ostream &operator <<(ostream &os, const Warriors& warrior);
};
// Here are my function prototypes
void open_file(ifstream& warriorfile);
void Warriorcommand(vector& warriors, const string& Warrior_name, int Warrior_strength, const Weapon& sword);
void battlecommand(vector& warriors, const string& Warrior_name1, const string& Warrior_name2);
void statusupdate(const vector& warriors);
// my main function
int main(){
ifstream warriorfile;
open_file(warriorfile);
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 sword(weaponName); // This will create a Weapon object
Warriorcommand( warriors, name, strength, sword);
} else if (command == "Battle"){
string Warrior_name1, Warrior_name2;
warriorfile >> Warrior_name1>> Warrior_name2;
battlecommand(warriors, Warrior_name1, Warrior_name2);
} else if (command == "Status"){
statusupdate(warriors);
}
}
return 0;
warriorfile.close();
}
void open_file(ifstream& warriorfile){
warriorfile.clear();
warriorfile.open("warriors.txt");
if(!warriorfile){
cerr << "Error opening warrior.txt"<< endl;
return;
warriorfile.close();
}
}
void Warriorcommand(vector &warriors, const string &Warrior_name, int Warrior_strength, const Weapon &sword){
for(size_t i =0; i < warriors.size(); i++){
if(warriors[i].get_name()== Warrior_name){
cerr <<" error" << Warrior_name <<""<< endl;
return;
}
}
warriors.emplace_back(Warrior_name, Warrior_strength, sword);
}
void battlecommand(vector& warriors, const string& Warrior_name1, const string& Warrior_name2){
int index1=-1;
int index2=-1;
for(int i =0; i < warriors.size(); i++){
if(warriors[i].get_name()== Warrior_name1){
index1= i;
}
if(warriors[i].get_name()== Warrior_name2){
index2= i;
}
}
if (index1==-1){
cerr << "Battle command not found" << endl;
if (index2==-1){
cerr << "Battle command not found" << endl;
return;
}
}
Warriors &warrior1= warriors[index1];
Warriors &warrior2= warriors[index2];
cout << warrior1.get_name()<< warrior2.get_name()<< endl;
int strength1= warrior1.get_strength();
int strength2= warrior2.get_strength();
if(strength1==0 && strength2==0){
cout << "Both warriors have died" << endl;
} else if (strength1==0){
cout << "This warrior right here is dead dude" <& warriors){
cout << "Warrior status update" << warriors.size()<< "warriors" << endl;
for( const Warriors& warrior : warriors){
cout << warrior.get_name()<<""<< warrior.get_strength()<< endl;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!