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 DDrummond Ann MMaxwell 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
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
This is my answer, but its not correct. How to fix it.
#include
#include
#include
#include
#include
using namespace std;
int main(){
string name[3];
int tmpScore, studentTotal, minScore, maxScore, countStudent, countClass;
ifstream studentDat;
ifstream namelist;
double classTotal;
minScore = 100;
maxScore = 0;
classTotal = 0.0;
countClass = 0;
cout << setw(20) << left << "students";
cout << setw(5) << left << "Data" << endl;
while(!namelist.eof()){
for(int i=0;i<3;i++){
namelist >> name[i];
}
studentDat.open((name[1]+name[0]+".dat").c_str());
studentTotal = countStudent = 0;
while(!studentDat.eof()){
studentDat >> tmpScore;
if(tmpScore>maxScore){
maxScore = tmpScore;
}
if(tmpScore>maxScore){
minScore = tmpScore;
}
studentTotal += tmpScore;
countStudent ++;
}
studentDat.close();
classTotal += (double)studentTotal / (double)countStudent;
countClass++;
cout< < < < cout< <<(double)studentTotal/(double)countStudent
< }
cout << setw(20)< cout << (double)classTotal/(double)countClass
< cout << setw(20) << left << "Max Score:";
cout << (double)maxScore << endl;
cout << setw(20) << left << "min Score:";
cout << (double)minScore << endl;
namelist.close();
return 0;
}