Hello! A few days ago, I asked for help with a Class Average problem I was assigned, and have now worked through the code, but I can't get the right output! I'm going to copy and paste the directions, and an outline my professor gave us to help, then the code, so I hope someone can see where I've messed up. Thanks for the help!
Write a C++ program that will prompt the user for the name of a data file. This data file contains a list of student names (unknown number) 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 single space.
Each student has a data file which contains an unknown number of test scores. The name of each student’s data file is created by concatenating the student's first, middle initial and last name and then appending the suffix “.dat”. For example, if the student's name is Bobby B Baylor, the data file with this student's grades is called BobbieBBaylor.dat. If the input file cannot be found, then display "Cannot open " + filename.
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, the program should report the following statistics for the class : total number of students, average, maximum and minimum of the students' averages. If a student grade file is missing, then print "Cannot open " + student file name" and do not include the student's info in the class statistics.
Note: All statistics should be rounded to the nearest hundredth.
cout << fixed << setprecision(2);
Example:
Data File: students.dat
Flintstone Fred D
Rubble Barney E
Slate Walter A
FredDFlintstone.dat
85
88
92
86
77
32
BarneyERubble.dat
95
72
88
89
WalterASlate.dat
68
75
83
Sample Program Output #1 (bad file name):
Enter name of data file: stduent.dat
Cannot open stduent.dat
Sample Program Output #2 (all student files present):
Enter name of data file: students.dat<enter>
Student Statistics
Fred D. Flintstone : 76.67
Barney E. Rubble : 86.00
Walter A. Slate : 75.33
Class Statistics
Total Students : 3
Class Average : 79.23
Max Average : 86.00
Min Average : 75.33
Sample Program Output #3 (one student file missing):
Enter name of data file: students.dat
Student Statistics
Fred D. Flintstone : 76.67
Barney E. Rubble : 86.00
Walter A. Slate : Cannot open WalterASlate.dat
Class Statistics
Total Students : 2
Class Average : 80.40
Max Average : 86.00
Min Average : 76.67
And, here's the outline:
1. Initialize counters / accumulators
a. Number of students
b. Summary of class grades
c. Number of class grades
2. Prompt user for file name for the file with the list of student names
3. If file cannot be opened, then display “Cannot open “ message
4. Else
a. Loop through each line in input file until all students are processed
i. Format file name for student
ii. Print student name (no new line yet)
iii. Attempt to open student grade file
iv. If file cannot be opened, then display “cannot open” message
v. Else
1. Increment number of students
2. Reset student grade sum and counter to 0
3. Loop through student’s grades until all grades are processed
a. Sum the grades
b. Count the number of grades
4. Calculate student average
5. Display student average
6. Assign maximum average if 1st student or current average is higher
7. Assign minimum average if 1st student or current average is lower
8. Increment summary of class grades
9. Increment number of class grades
b. Calculate class average
i. If grade count > 0, calculate average ( sum of all grades / total number of grades)
c. Display the class statistics
i. If grade count = 0, display “No students”
ii. Else display the statistics
1. Number of students
2. Class average
3. Maximum average
4. Minimum average
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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
#include <iostream> //for cout and cin
#include <iomanip> //for setprecision and setw
#include <fstream> //for ifstream and ofstream
//Initialize counters / accumulators
int numberOfStudents;
int summaryClassGrades;
int numberClassGrades;
int classAverage;
int totalGrade;
int maxAverage;
int minAverage;
int studentGrade;
int studentAverage;
int grade;
char lName [20];
char fName [20];
char mInitial [20];
const double HEADER_WIDTH = 25;
using namespace std;
int main()
{
char fileName [50];
ifstream inputFile;
ifstream students;
inputFile.open ("students.dat");
//Prompt user for file name for the file with the list of student names
cout << "Enter name of data file:" << endl;
cin >> fileName;
cout << fileName << endl;
//If file cannot be opened, then display ÒCannot open Ò message
if (inputFile.fail())
{
cout << "Cannot open student.dat" << endl;
}
else
{ //Loop through each line in input file until all students are processed
while (students >> lName >> fName >> mInitial)
{
// format student grade file name
ofstream studentsFileName;
//Print Student Name
cout << "Student Statistics";
cout << "\t" << left << setw(HEADER_WIDTH)
<< studentsFileName << ": "
<< fixed << setw(2) << setprecision(2) << studentAverage
<< endl;
// open student grade file
students.open("studentsFileName.c_str()");
if (students.fail())
{ //If file cannot be opened, then display Òcannot openÓ message
cout << "Cannot open " << studentsFileName << endl;
}
else
{ //Increment number of students
numberOfStudents++;
//Reset student grade sum and counter to 0
int gradeSum = 0;
int gradeCount = 0;
//Loop through student's grades until all grades are processed
while (studentGrade)
{
gradeSum++;
gradeCount++;
}
//Calculate student average
studentAverage = studentGrade / numberClassGrades;
//Display student average
cout << studentAverage << endl;
//Assign maximum average if 1st student or current
//average is higher
if (studentAverage >= 0)
{
cout << maxAverage;
}
//Assign minimum average if 1st student or current average is lower
else
{
cout << minAverage;
}
//Increment summary of class grades
summaryClassGrades++;
//Increment number of class grades
numberClassGrades++;
}
//Calculate class average
classAverage = summaryClassGrades / totalGrade;
if (numberClassGrades > 0)
{
cout << classAverage;
}
if (numberClassGrades = 0)
{
cout << "No Students" << endl;
}
else
{
cout << "Total Students " << "\t :"
<< numberOfStudents << endl;
cout << "Class Average " << "\t :"
<< classAverage << endl;
cout << "Max Average" << "\t :"
<< maxAverage << endl;
cout << "Min Average" << "\t :"
<< minAverage << endl;
students.close();
}
}
inputFile.close();
return 0;
}}
|