CS1440 Lab (7)
April 2, 2001

You will learn to read from or write to a file that will be specified by the user.

Before you begin the lab, create the directory lab7.  Then copy the entire contents of the directory lab7 from the instructor home directory.  Assuming that you are already in the directory lab7, here is the command that you will use to copy the files:

% cp ~rt/public/lab7_files/*  .

Write C++ code to do the following:

Reads an input file that stores the number of credit hours and the semester grades for a student.  Each file contains two columns of data.  The first column (an integer) represents number of credit hours and the second column (a float) represents the grade.

You are to find the semester grade point average for each student by dividing that sum of all grade point by the total number of credit hours.

Also, your program detects computes the maximum and minimum of grade for each student.

I will provide you three files containing the grades for three students, Tom, Jack, and Lena.  These files are called: 1) tom.dat, 2) jack.dat, and 3) lena.dat.  The program is to ask the user to enter the names for the input and the output files. Your code have to display a message if it failed to open those files and then exit. The program will read all data that is in the file.  It uses the eof() member function to detect the end of the file.  Please see the example at the next page to learn how to use the eof() member function.

The first column is the credit hour and the second one is the grade.  The maximum possible grade is 4.0 and the minimum is zero.   If a grade is less than 0.0, set the grade to zero, display a message, and continue the calculations.  If a grade is larger than 4.0, set the grade to 4.0 and display a message regarding the change.

Sample input file (tom.dat):
3  3.0
2  4.0
1  4.0

Sample calculation using the data in the tom.dat file:
Sum = 3*3.0 + 2*4.0 + 1*4.0 = 21.0
Average = 21.0/6 =  3.5

Sample input:
Please enter the name of the input file:  tom.dat
Please enter the name of the output file:  tom.out

Sample output (tom.out):
The sum of all grades is: 21.0
The total credit hours are: 6
The Grade Point Average for this student is: 3.5
The minimum grade for this student is: 3.0
The maximum grade for this student is: 4.0

How to read until the end of a  file

Suppose in_s is your input stream and you have the variables grade as float and credit_hour as integer to represent the grade and the number of credit hour respectively.

in_s >> credit_hour >> grade;

while( ! in_s.eof() )
       .  Compute the cumulative sum and credit hours here

       in_s >> credit_hour >> grade;