In: Computer Science
Retrieve program Grades.cpp and the data file graderoll.dat from the Lab 12 folder. The code is as follows:
______________________________________________________________________________
#include // FILL IN 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;
};
typedef Grades gradeType[MAXRECORDS];
// This makes gradeType a data type
// that holds MAXRECORDS
// Grades structures.
// FIll IN THE CODE FOR THE PROTOTYPE OF THE FUNCTION ReadIt
// WHERE THE FIRST ARGUMENT IS AN INPUT FILE, THE SECOND IS THE
// ARRAY OF RECORDS, AND THE THIRD WILL HOLD THE NUMBER OF RECORDS
// CURRENTLY IN THE ARRAY.
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;
}
// FILL IN THE CODE TO CALL THE FUNCTION ReadIt.
// output the information
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 << endl;
}
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(// FILL IN THE CODE FOR THE FORMAL PARAMETERS AND THEIR
// DATA TYPES.
// inData, gradeRec and total are the formal parameters
// total is passed by reference)
{
total = 0;
inData.get(gradeRec[total].name, NAMESIZE);
while (inData)
{
// FILL IN THE CODE TO READ test1
// FILL IN THE CODE TO READ test2
// FILL IN THE CODE TO READ final
total++; // add one to total
// FILL IN THE CODE TO CONSUME THE END OF LINE
// FILL IN THE CODE TO READ name
}
}
______________________________________________________________________________
Exercise 1: 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. Notice that in the data file the names occupy no more than 15 characters. Why?
Sample Run:
Exercise 2: 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
Sample Run:
Working code implemented in C++ and appropriate comments provided for better understanding:
Source code for Grades.cpp:
#include <fstream>
#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.
// Michael Steele
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;
// This makes gradeType a data type
// that holds MAXRECORDS
// Grades structures.
// FIll IN THE CODE FOR THE PROTOTYPE OF THE FUNCTION
ReadIt
// WHERE THE FIRST ARGUMENT IS AN INPUT FILE, THE SECOND IS
THE
// ARRAY OF RECORDS, AND THE THIRD WILL HOLD THE NUMBER OF
RECORDS
// CURRENTLY IN THE ARRAY.
void readIt(ifstream &,gradeType *, int &);
char letter(int test1,int test2, int final);
int main()
{
ifstream indata;
indata.open("graderoll.dat");
int numRecord; // number of records read in
gradeType studentRecord[MAXRECORDS];
if (!indata)
{
cout << "Error opening file.
\n";
cout << "It may not exist
where indicated" << endl;
return 1;
}
readIt(indata,studentRecord,numRecord);
// output the information
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 << " " << "Final
grade " << studentRecord[count].letter << endl;
}
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)// FILL IN THE CODE FOR THE FORMAL PARAMETERS AND
THEIR
// DATA TYPES.
// inData, gradeRec and total are the formal
parameters
// total is passed by reference)
{
total = 0;
indata.get(gradeRec[total].name, NAMESIZE);
while (indata)
{
// FILL IN THE CODE TO READ
test1
indata >>
gradeRec[total].test1;
// FILL IN THE CODE TO READ
test2
indata >>
gradeRec[total].test2;
// FILL IN THE CODE TO READ
final
indata >>
gradeRec[total].final;
gradeRec[total].letter =
letter(gradeRec[total].test1,gradeRec[total].test2,gradeRec[total].final);
total++; // add one to
total
// FILL IN THE CODE TO CONSUME
THE END OF LINE
char eat;
indata.get(eat);
// FILL IN THE CODE TO READ
name
indata.get(gradeRec[total].name,
NAMESIZE);
}
}
char letter(int test1,int test2, int final)
{
int avg = 0.3 * test1 + 0.3 * test2 +0.4 *
final;
if(avg >= 90)
return 'A';
else if(avg >= 80)
return 'B';
else if(avg >= 70)
return 'C';
else if(avg >= 60)
return 'D';
else
return 'F';
}
Sample Output Screenshots:
Hope it helps, if you like the answer give it a thumbs up. Thank you.