Complete the program by filling in the code (areas in bold). This problem requires
that you study very carefully the code and the data file already written to prepare you to complete the program.Add another field called letter to the record which is a character that holds the letter grade of the student. This is based on the average of the grades as follows: test1 and test2 are each worth 30% of the grade while final is worth 40% of the grade. The letter grade is based on a 10 point spread. The code will have to be expanded to find the average.
90-100 A
80-89 B
70-79 C
60-69 D
0-59 F
graderoll.dat file
Dean DeFino 88 98 99
Sally Johnson 78 89 82
Bill Benny 75 79 81
Thomas Billows 78 84 89
#include <fstream> // DIRECTIVE FOR FILES
#include <iostream>
#include <iomanip>
using namespace std;
// This program reads records from a file. The file contains the
// following: student’s name, two test grades and final exam grade.
// It then prints this information to the screen.
const int NAMESIZE = 15;
const int MAXRECORDS = 50;
struct Grades // declares a structure
{
char name[NAMESIZE + 1];
int test1;
int test2;
int final;
char letter;
};
typedef Grades gradeType[MAXRECORDS];
void readIt(ifstream& inData, gradeType gradeRec,int& total);
void getGrade(gradeType gradeRec,int total);
int main()
{
ifstream inData;
inData.open("graderoll.dat");
int numRecord; // number of records read in
gradeType studentRecord;
if(!inData)
{
cout << "Error opening file. n";
cout << "It may not exist where indicated" << endl;
return 1;
}
readIt(inData,studentRecord,numRecord);
getGrade(studentRecord,numRecord);
for (int count = 0; count < numRecord; count++)
{
cout << studentRecord[count].name << setw(10)
<< studentRecord[count].test1
<< setw(10) << studentRecord[count].test2;
cout << setw(10) << studentRecord[count].final;
cout << setw(10) << studentRecord[count].letter << endl;
}
cin>>numRecord;
return 0;
}
//**************************************************************
// readIt
//
// task: This procedure reads records into an array of
// records from an input file and keeps track of the
// total number of records
// data in: data file containing information to be placed in
// the array
// data out: an array of records and the number of records
//
//**************************************************************
void readIt(ifstream& inData, gradeType gradeRec,int& total){
total = 0;
inData.get(gradeRec[total].name, NAMESIZE);
while (inData)
{
if(total<MAXRECORDS){
// READ test1
inData >> gradeRec[total].test1;
// READ test2
inData >> gradeRec[total].test2;
// READ final
inData >> gradeRec[total].final;
total++; // add one to total
// CONSUME THE END OF LINE
inData.ignore(NAMESIZE, '\n');
// READ name
inData.get(gradeRec[total].name, NAMESIZE);
}
}
}
void getGrade(gradeType gradeRec,int total){
for(int i=0;i<total;i++){
double averageGrades=gradeRec[i].test1*0.3+gradeRec[i].test2*0.3+gradeRec[i].final*0.4;
if(averageGrades>=90){
gradeRec[i].letter= 'A';
}else if(averageGrades>=80){
gradeRec[i].letter= 'B';
}else if(averageGrades>=70){
gradeRec[i].letter= 'C';
}else if(averageGrades>=60){
gradeRec[i].letter= 'D';
}else{
gradeRec[i].letter= 'F';
}
}
}
Example:
Comments
Leave a comment