Hello everyone,
I am having a really difficult time getting this program off of the ground. I need to use a while loop to read data from a file and then read tonnage numbers corresponding to names read in from that file. The assignment looks something like this:
After watching the Discovery Channel’s, Ice Road Truckers, German transportation tycoon Otto Bonn is expanding his business to North America. He has been taking resumes from truck drivers through out the U.S. and Canada. He has collected a large number of files that contain the per season tonnage transported for several drivers, and he is asking you to write a C++ program that will ask the user for the name of a data file. This data file contains a list of drivers. 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 driver has a data file that contains an unknown number of seasons. The name of each driver’s data file is formed by concatenating the drivers first and last name and adding “.dat”, for example:
Driver Name: Nowak Alex D
Driver Data File: AlexNowak.dat
Your program should open each driver’s data file, read in the tonnage and calculate the driver’s average tonnage per season. In addition to calculating the average tonnage for each driver you should report the average for all drivers, and the highest and lowest tonnage.
Note: All tonnage averages should be rounded to the nearest hundredth.
cout << fixed << setprecision(2);
Example:
Data File: Drivers.dat
Nowak Alex D
Bodeau Hugh M
Rick Godon L
AlexNowak.dat
412
239
405
365
HughBodeau.dat
183
150
235
368
RickGodon.dat
329
450
312
249
Sample Program Output:
Enter Name of Data File: Drivers.dat<enter>
Drivers Average
Alex M Nowak 355.25
Hugh M Bodeau 234.00
Rick L Godon 335.00
Tonnage Average: 308.08
Max Tonnage: 450
Min Tonnage: 150
Corrections:
have a correction to make to the example.
Drivers.dat should contain:
Nowak Alex D
Bodeau Hugh M
Godon Rick L
I also have a helpful bit of information. There is a possiblilty that you will need to reuse an ifstream in this assignment. To reuse the ifstream, you must close and clear the ifstream. For example:
ifstream infile;
/ code that does stuff here.
infile.close();
infile.clear();
This is necessary because we can think of the file streams as containers that record information about the files that we have opened. In addition to storing the name of the file, the file streams record errors that have occurred when using the file. Unfortunately, this error information is not reset with the close() method alone. If a file stream object is used to read to the end of the file, a error is set indicating that the end of the file has been reached, this is how the eof() method determines that the end of the file has been reached. Later, if we try to open a different file using the same file stream, a test of the open will result with a failure because the file stream believes that it has reached the end of the file. We must use the clear() meathod to clear the error status of the file stream to reuse it.
What I am asking for is help getting this program started. I don't understand how to correctly read in these names if the user is naming the file. Any help is greatly appreciated.