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.

1. ask the user for the master filename of all the truckers.
2. read that master filename into a string.
3. open the file with that master filename
4. set up a while loop
5. Read in a name from the master file
6. Get the filename associated with that name by string manipulation on that name.
7. Open up the file with that filename.
8. Do stuff with that file.
9. Close the file from 7.
10. Repeat 5 - 9.
11. Close the master file.

string masterFilename; // contains master filename.
// read in user input into masterFilename
ifstream masterFile;
masterFile.open (masterFilename.c_str ());

Thank you, that is extremely helpful. I am going to put together some code and then post back what I come up with. Thanks again.

Alright everyone,

The program I currently have running has a while-loop with an embedded while-loop. I now need to calculate the max and min, but the only way I can come up with to do so is using conditional statements where I take the values I know and compare them. I don't know how to use arrays, so I don't think I can use them. We are just learning loops right now. If anyone has any suggestions, I would appreciate them.

Use a variable to keep the current min and another varaible to keep the current max. As you encounter a new vaule compare it to both the current min and the current max. If the current value is higher than the current max, it becomes the new max, and if it is lower than the current low, it becomes the new low. At the end of the loop the current min and current max are the min and max inputs respectively.

This is essentially what you already planned to do. No other way to do it that I know of without arrays/containers of some sort.

I was thinking I should do it along those lines. In order to do this do I need to check every value that I read in? Should I compare every new value with the one stored at min and max?

Thanks

I have tried a variety of methods, but can't get the loop to store a max number. Can someone help?

while (infile >> lastName && infile >> firstName && infile >> mInitial)
	{
		
		revName = firstName+lastName+ext;
		infile2.open(revName.c_str());
		if (!infile2)
		{
			cout << "Error opening file. \n";
			return 1;
		}
		while (infile2 >> num)
		{

			if( num > num)
			{
				max = num;
			}

			sum += num;
			totSum += num;
			count1++;
			count2++;
		}
		avg = sum/count1;
		cout << firstName << " " << mInitial << " " << lastName << ":\t" << fixed 
			 << setprecision(2) << avg <<endl;
		infile2.close();
		infile2.clear();
		count1 = 0;
		num = 0;
		sum = 0;
	}

Here is what helped me if anyone else is having this same problem:

while (infile2 >> num)
		{

			if (count3 == 0) 
			{
				max = num;
				min = num;
			}
		    if (num > max) 
			{
				max = num;
		    }
		    if (num < min)
			{
				min = num;
		    }
		    count3++;


			sum += num;
			totSum += num;
			count1++;
			count2++;
		}
This question has already been answered. Start a new discussion instead.