Hi i am taking a C++ class and i am stuck on this assignment can anyone please help. For the assignment i have to modify
Posted: Thu Jul 14, 2022 2:27 pm
Hi i am taking a C++ class and i am stuck on this assignment cananyone please help.
For the assignment i have to modify the code below
#include<iostream>
#include<iomanip>
#include<fstream>
//using namespace
using namespace std;
//struct Bowler
struct Bowler
{
string name;
int scores[4]{};
int averageScore{};
};
//declare the constants and functions
//const int cols = 5, rows = 10;
bool GetBowlingData(const string& filename, Bowlerbowlers[], int& count);
void GetAverageScore(Bowler bowlers[], int count);
void PrettyPrintResults(Bowler bowlers[], int count);
int main()
{
//declare the variables
int count = 0;
//filename, rows, and columns
string filename = "BowlingScores.txt";
Bowler bowlers[100];
//the precision
cout << setprecision(2) << fixed <<showpoint;
//call the functions
if (GetBowlingData(filename, bowlers, count))
{
GetAverageScore(bowlers, count);
PrettyPrintResults(bowlers, count);
}
//if file not opened
else
{
cout << "** File Not Found **" << endl;
}
return 0;
}
//getBowlingData to read the bowling scores txt file
bool GetBowlingData(const string& filename, Bowlerbowlers[], int&count)
{
//input stream for the file
ifstream dataIn;
//open the input file
dataIn.open(filename.c_str());
//check the file name
if (dataIn.fail())
{
cout << "**File Not Found **";
return false;
}
else
{
//count the records in file
while (dataIn >> bowlers[count].name)
{
for (int& score : bowlers[count].scores)
{
dataIn >> score;
}
count++;
}
//cose the file
dataIn.close();
}
return true;
}
//GetAverageScore to calculate the average
void GetAverageScore(Bowler bowlers[], int count)
{
double total;
//for loop to read the scores of each bowler
for (int i = 0; i < count; i++)
{
total = 0;
for (int score : bowlers.scores)
{
total += score;
}
//calculate average
bowlers.averageScore = (int)(total / 4);
}
}
//display the output
void PrettyPrintResults(Bowler bowlers[], int count)
{
//header
cout <<setw(14) << left << "Name" <<setw(8) <<left <<"Score1\tScore2\tScore3\tScore4\tAverage" << endl;
for (int i = 0; i < count; i++)
{
cout << setw(14) << left <<bowlers.name;
for (int score : bowlers.scores) {
cout << score<< "\t";
}
cout << setw(5) << bowlers.averageScore <<endl;
}
}
For the assignment i have to modify the code below
#include<iostream>
#include<iomanip>
#include<fstream>
//using namespace
using namespace std;
//struct Bowler
struct Bowler
{
string name;
int scores[4]{};
int averageScore{};
};
//declare the constants and functions
//const int cols = 5, rows = 10;
bool GetBowlingData(const string& filename, Bowlerbowlers[], int& count);
void GetAverageScore(Bowler bowlers[], int count);
void PrettyPrintResults(Bowler bowlers[], int count);
int main()
{
//declare the variables
int count = 0;
//filename, rows, and columns
string filename = "BowlingScores.txt";
Bowler bowlers[100];
//the precision
cout << setprecision(2) << fixed <<showpoint;
//call the functions
if (GetBowlingData(filename, bowlers, count))
{
GetAverageScore(bowlers, count);
PrettyPrintResults(bowlers, count);
}
//if file not opened
else
{
cout << "** File Not Found **" << endl;
}
return 0;
}
//getBowlingData to read the bowling scores txt file
bool GetBowlingData(const string& filename, Bowlerbowlers[], int&count)
{
//input stream for the file
ifstream dataIn;
//open the input file
dataIn.open(filename.c_str());
//check the file name
if (dataIn.fail())
{
cout << "**File Not Found **";
return false;
}
else
{
//count the records in file
while (dataIn >> bowlers[count].name)
{
for (int& score : bowlers[count].scores)
{
dataIn >> score;
}
count++;
}
//cose the file
dataIn.close();
}
return true;
}
//GetAverageScore to calculate the average
void GetAverageScore(Bowler bowlers[], int count)
{
double total;
//for loop to read the scores of each bowler
for (int i = 0; i < count; i++)
{
total = 0;
for (int score : bowlers.scores)
{
total += score;
}
//calculate average
bowlers.averageScore = (int)(total / 4);
}
}
//display the output
void PrettyPrintResults(Bowler bowlers[], int count)
{
//header
cout <<setw(14) << left << "Name" <<setw(8) <<left <<"Score1\tScore2\tScore3\tScore4\tAverage" << endl;
for (int i = 0; i < count; i++)
{
cout << setw(14) << left <<bowlers.name;
for (int score : bowlers.scores) {
cout << score<< "\t";
}
cout << setw(5) << bowlers.averageScore <<endl;
}
}