Question: can you modify this code to add. Your program must use one structure that contains the following member variables: a string data type that will

can you modify this code to add.
Your program must use one structure that contains the following member variables:
a string data type that will hold the name of the bowler.
a one-dimensional array of integers to store the bowler's 4 scores.
an integer to hold the average bowling score.
Your program must create and use an array of the structures mentioned previously.
Your program must contain at least the following functions:
a Boolean returning function to read and store data into an array of structures. The function should take as input parameters the file name and an empty array of structures, which it should fill from the file (BowlingScores.txt) and return a status of either success or failure (i.e., true or false).
a void function that is used to calculate the average bowling score. The function should take as input the array of structures which it should fill with the average score of each bowler.
a void function to output the results, i.e., bowler name, scores and average to the console and to a file named: scores.dat
Your program output must look identical to the image below or your score will be reduced.
#include
#include
#include
using namespace std;
#define SIZE 100
#define COL 4
//this opens the file with data
bool GetBowlingData(string file, string names[], int data[][COL]){
ifstream fin(file.c_str());
if (!fin){
return false;
}
int count =0;//this goes in and finds the data and puts it into a varabile
while (fin >> names[count]>> data[count][0]>> data[count][1]>> data[count][2]>> data[count][3])
count++;
return true;
}
double* GetAverageScore(int data[][COL]){
int count =0;
for (int i =0; i < SIZE; i++){
if (data[i][0]!=-1)
count++;
else
break;
}
double* avg = new double[count];//this finds the average score
for (int i =0; i < count; i++){
avg[i]=(data[i][0]+ data[i][1]+ data[i][2]+ data[i][3])/4.0;
}
return avg;
}
void PrettyPrintResults(string names[], int data[][4], double avg[]){
int count =0;
for (int i =0; i < SIZE; i++){
if (data[i][0]!=-1)
count++;
else
break;
}//this prints it
for (int i =0; i < count; i++){
cout << names[i]<<"......"<< data[i][0]<<""<< data[i][1]<<""<< data[i][2]<<""<< data[i][3]<<""<< avg[i]<< endl;
ofstream outfile("scores.dat");// makes a file of score data.
if (outfile.is_open()){
outfile << names[i]<<"......"<< data[i][0]<<"......."<< data[i][1]<<"......."<< data[i][2]<<"......."<< data[i][3]<<"......"<< avg[i]<< endl;
outfile.close();
}
else
{
cerr << "Unable to open file." << endl;//this is if the file fails to open
}
}
}
int main(){
//this makes the arrays you need
string names[SIZE];
int data[SIZE][COL];
double* avg;
//this puts it into an array
for (int i =0; i < SIZE; i++){
for (int j =0; j < COL; j++){
data[i][j]=-1;
}
}
for (int i =0; i < SIZE; i++){
names[i]= "Unknown";
}
//this gets the avg scores from arrays you put in and opens file
bool status = GetBowlingData("BowlingScores.txt", names, data);
if (!status){
cout << "Error with the file
";
return 0;
}
avg = GetAverageScore(data);
PrettyPrintResults(names, data, avg);
return 0;

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 Databases Questions!