The objective of this program is to ask the user for the name of a data file.
If the file does not open, exit the program.
This data file contains a list of students in a 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”.
I believe that I have that part works. This is the part that I am having issues with:
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.
Other Requirements
If the student file fails to open - print the error message "No Data File".
If there are no grades in the file - print the message "No Grades".
This is the code that I have so far, I could really use some help. I just can't get it to run correctly it's giving me a return of 1.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
#include<iostream>
#include<iomanip>
#include<fstream>
using namespace std;
// Driver program to test
int main()
{
string dataFileName;
string lastname, firstname, middleIntial, student_file, fullname;
double min_score;
double max_score;
double total_score = 0;
int student_count = 0;
int count = 0;
double score = 0;
cout<<"Please Enter the Name of a Data File: ";
cin>>dataFileName;
cout<<dataFileName<<endl;
ifstream openDataFile(dataFileName);
if(!openDataFile.is_open())
{
cout<<"Error: File Not Open\n";
exit(EXIT_FAILURE);
}
cout << left << setw(30) << "Students" << "Average" << endl;
while( openDataFile >> lastname >> firstname >> middleIntial)
{
fullname = firstname + " " + middleIntial + " " + lastname;
student_file = firstname + lastname + ".dat";
// Opening student file for reading test scores
ifstream s_file( student_file );
int temp;
while( s_file >> temp)
{
score += temp;
if ( temp < min_score )
min_score = temp;
else if ( temp > max_score )
max_score = temp;
count++;
}
double average = score / count ;
cout << left << setw(30)
<< fullname << fixed << setprecision(2)
<< average << endl;
total_score += average;
student_count++;
s_file.close();
}
double class_avg = total_score / student_count;
cout << left << setw(30) <<"Class Average: " <<fixed << setprecision(2) << class_avg << endl;
cout << left << setw(30) <<"Max Score: " << fixed << setprecision(2) << max_score << endl;
cout << left << setw(30) <<"Min Score: " << fixed << setprecision(2) << min_score << endl;
openDataFile.close() ;
return 0;
}
|
This is a sample txt file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
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
|
this is what my output This is the output I get. to me it seems like it's reading the file, and printing it not processing the .dat info:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
Students Average
Dennis D Tuel nan
Ann M Drummond nan
Virginia L Maxwell nan
85 88 DennisTuel.dat nan
86 AnnDrummond.dat 92 nan
72 88 95 nan
VirginiaMaxwell.dat 68 89 nan
83 98 75 nan
Class Average: nan
Max Score: 0.00
Min Score: 0.00
|
this is what it is supposed to be:
1 2 3 4 5 6 7 8 9 10 11
|
Enter Name of Data File: Students.dat <enter>
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
|