Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C++ using File Processing to write a program that allows you to write stats o

ID: 3836965 • Letter: I

Question

In C++ using File Processing to write a program that allows you to write stats of a monster and save it to a file. (in text format) (Things like Name, Hit Points, Minimum damage and Maximum damage.) Allows you to open the file and read the stats of the monster. (Print what was read onto the screen.) Allow the user to decide what file to open (just type in the filename)

Allow the program to load the stats of two monsters and let them battle it out.

1. Create monster stats

2. Ask the user if creating or loading (ask the user the file name)

3a. If loading open file for reading read stats in from file print stats to screen

3b. If creating open file for writing write stats to file - make sure to leave spaces or returns between variables, so all values do not get smooshed together

Explanation / Answer

/**************** Sample Output ****************/

// store the file with name monster.cpp
// $ g++ monster.cpp
// $ ./a.out
// Enter 1 for Creating and 2 for Loading
// 1
// Enter name, hit points, minimum damage and maximum damage
// dog 8 12.67 45.2
// $ cat monster_stats.txt
// dog 8 12.67 45.2
// $ ./a.out
// Enter 1 for Creating and 2 for Loading
// 2
// dog 8 12.67 45.2


#include <iostream>
#include <fstream>

using namespace std;

// declare monster stat class to store stat of monster
class MonsterStat {
private:
   string name;           // name stores the name of the monster
   double hit_points;       // hit_points store the hit points
   double min_damage;       // min_damage store minimum damage
   double max_damage;       // max_damage store maximum damage

public:
   // contructor for default values
   MonsterStat(){
       name = "";
       hit_points = 0;
       min_damage = 0;
       max_damage = 0;
   }
   // constructor with parameters
   MonsterStat(string name, double hit_points, double min_damage, double max_damage){
       this -> name = name;
       this -> hit_points = hit_points;
       this -> min_damage = min_damage;
       this -> max_damage = max_damage;
   }

   // getter method to get name of monster
   string getName(){
       return name;
   }
   // getter for hit points
   double getHitPoints(){
       return hit_points;
   }
   // getter for minimum damage
   double getMinDamage(){
       return min_damage;
   }
   // getter for maximum damage
   double getMaxDamage(){
       return max_damage;
   }
};

int main(){

   string name;
   double hit_points, min_damage, max_damage;
   int response;

   // first get the use response 1 for creating and 2 for loading
   cout << "Enter 1 for Creating and 2 for Loading ";
   cin >> response;
  
   if(response == 1){
      
       // if response is 1 then open a text file named monster_stats in write mode
       ofstream outfile;
       outfile.open("monster_stats.txt");
       // get monster information from user
       cout << "Enter name, hit points, minimum damage and maximum damage ";
       cin >> name >> hit_points >> min_damage >> max_damage;
       // create monster object
       MonsterStat ms(name, hit_points, min_damage, max_damage);
       // write monster information into the file
       outfile << ms.getName() << " " << ms.getHitPoints() << " " << ms.getMinDamage() << " " << ms.getMaxDamage() << endl;
       // finally close the file
       outfile.close();
  
   }else if( response == 2){
       // if response is 2 then open the same file in reading mode
       ifstream infile;
       infile.open("monster_stats.txt");
       // get the infromation of monster from the file
       infile >> name >> hit_points >> min_damage >> max_damage;
       // after getting the information output into the console or terminal
       cout << name << " " << hit_points << " " << min_damage << " " << max_damage << endl;

   }else{
       // if response is neither 1 or 2 then print invalid response into the terminal
       cout << "Invalid response ";
  
   }
   return 0;
}