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

Program 13 \"Class Average\" [hide] Program 13 \"Class Average\" Write a C++ pro

ID: 672973 • Letter: P

Question

Program 13 "Class Average" [hide]

Program 13 "Class Average"

Write a C++ program that will ask the user for the name of a data file.
This data file contains a list of students in some class.
The names are listed, one per line, in the following format:

lastName firstName middleInitial

and each part of the name is separated by a space.

Each student in this class has a data file that contains an unknown number of test scores. The name of each student’s data file is formed by concatenating the students first and last name and adding “.dat”, for example:


Student Name: Tuel Dennis D

Student Data File: DennisTuel.dat

Your program should open each student’s data file, read in the scores and calculate the student’s average.
In addition to calculating the average for each student you should report the average for the class, and the highest and lowest test score in the class.

Note: All averages should be rounded to the nearest hundredth.

cout << fixed << setprecision(2);

Example:

Data File: Students.dat

Tuel Dennis D

Drummond Ann M

Maxwell Virginia L

DennisTuel.dat

85

88

92

86

AnnDrummond.dat

95

72

88

89

VirginiaMaxwell.dat

68

75

83

98

Sample Program Output:

Enter Name of Data File: Students.dat <enter>

68.00

The program above uses "String name[3]".

Now, I want to use "string lastName,   firstName, middleInitial".

How to rewrite a new part of those code?

Students Average Dennis D Tuel 87.75 Ann M Drummond 86.00 Virginia L Maxwell 81.00 Class Average: 84.92 Max Score: 98.00 Min Score

68.00

Explanation / Answer

#include <string>
#include <fstream>

class Student
{
std::string         lastName;
std::string         firstName;
std::string         middleInitial;
std::vector<double> scores;
double              average;

public:
Student( std::string line );
void parse();

private:
void readScores(std::ifstream& fin);
void calculateAverage();
};

class Class
{
std::string          m_filename;
std::vector<Student> m_students;
public:
void promptFile();
void parse();

private:
void addStudent( std::string line );
};


#include <iostream>
#include <sstream>

void Class::promptFile()
{
std::cout << "Filename: ";
std::cin >> m_filename;
}

void Class::parse()
{
std::ifstream fin( m_filename );
std::string line;

std::getline( fin, line );
while ( fin.good() )
{
    addStudent( line );
   std::getline( fin, line );
}
}

void Class::addStudent(std::string line)
{
try
{
    Student s(line);
    s.parse();
   m_students.push_back(s);
}
catch (... ) { }
}

Student::Student(std::string Line)
{
std::stringstream iss(Line);
std::getline( iss, lastName, ' ');
std::getline( iss,firstName, ' ');
std::getline( iss,middleInitial, ' ');
}

void Student::parse()
{
std::string filename =firstName +middleInitial + lastName + ".dat";

std::ifstream fin(filename);

if (! fin.is_open() )
{
    std::cout << "Cannot open " << filename << std::endl;
   throw 1;
}

readScores( fin );
calculateAverage();
}

void Student::readScores(std::istream& fin)
{
std::copy( std::istream_iterator<double>(fin), std::istream_iterator<double>(), std::back_inserter(scores) );
}

void Student::calculateAverage()
{
m_average = 0.0;
for (std::vector<double>::iterator it = scores.begin(); it != scores.end(); ++it)
    m_average += *it;
m_average /= scores.size();
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote