Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

can you modifey my code to using the following reqirements. Your program must define a class that contains the following member variables and functions: a

can you modifey my code to using the following reqirements.
Your program must define a class that contains the following member variables and functions:
a private string that contains the name of the file.
a private array of the same structure as defined in Week 07: Programming Assignment. The structure should also be defined privately inside the class.
a public constructor that initializes the name of the file data member. The name of the data file should be passed into the constructor as a parameter.
a public Boolean returning function to read and store data into the array of the structures data member. This function should take no parameters and return a status of either success or failure (i.e., true or false).
a public void function that is used to calculate the average bowling score. This function should take no parameters. It should fill the array of structures data member with the average score of each bowler.
a public void function to output the results, including file name, bowler name, scores and average, to the console and a file named scores.dat. This function should take no parameters.
Your program must also implement a main function that is used to create an instance of the class mentioned above and call its public functions to exercise/test the classes implementation.
#include
#include
#include
#include
using namespace std;
#define NUM_SCORES 4
// Structure to hold bowler's data
struct Bowler {
string name;
int scores[NUM_SCORES];
int average;
};
// Function to read and store data into an array of structures
bool GetBowlingData(string file, vector& bowlers){
ifstream fin(file.c_str());
if (!fin){
return false;
}
Bowler tempBowler;
while (fin >> tempBowler.name >> tempBowler.scores[0]>> tempBowler.scores[1]>> tempBowler.scores[2]>> tempBowler.scores[3]){
bowlers.push_back(tempBowler);
}
fin.close();
return true;
}
// Function to calculate the average bowling score
void CalculateAverages(vector& bowlers){
for (auto& bowler : bowlers){
int total =0;
for (int i =0; i < NUM_SCORES; ++i){
total += bowler.scores[i];
}
bowler.average = total / NUM_SCORES;
}
}
// Function to output the results to console and file
void PrettyPrintResults(const vector& bowlers){
ofstream outfile("scores.dat");
if (!outfile){
cerr << "Unable to open file scores.dat." << endl;
return;
}
for (const auto& bowler : bowlers){
cout << bowler.name <<"......"<< bowler.scores[0]<<""<< bowler.scores[1]<<""<< bowler.scores[2]<<""<< bowler.scores[3]<<""<< bowler.average << endl;
outfile << bowler.name <<"......"<< bowler.scores[0]<<""<< bowler.scores[1]<<""<< bowler.scores[2]<<""<< bowler.scores[3]<<""<< bowler.average << endl;
}
outfile.close();
}
int main(){
vector bowlers;
// Reading bowling data from file
if (!GetBowlingData("BowlingScores.txt", bowlers)){
cout << "Error with the file" << endl;
return 0;
}
// Calculating average scores
CalculateAverages(bowlers);
// Printing results to console and file
PrettyPrintResults(bowlers);
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

More Books

Students also viewed these Databases questions