| | |
File read-in with loops, max, and min.
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Sep 2008
Posts: 30
Reputation:
Solved Threads: 1
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.
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.
•
•
Join Date: Jan 2008
Posts: 3,837
Reputation:
Solved Threads: 503
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.
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.
C++ Syntax (Toggle Plain Text)
string masterFilename; // contains master filename. // read in user input into masterFilename ifstream masterFile; masterFile.open (masterFilename.c_str ());
•
•
Join Date: Sep 2008
Posts: 30
Reputation:
Solved Threads: 1
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.
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.
•
•
Join Date: Jul 2005
Posts: 1,755
Reputation:
Solved Threads: 283
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.
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.
Last edited by Lerner; Oct 3rd, 2008 at 5:49 pm.
•
•
Join Date: Sep 2008
Posts: 30
Reputation:
Solved Threads: 1
I have tried a variety of methods, but can't get the loop to store a max number. Can someone help?
C++ Syntax (Toggle Plain Text)
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; }
•
•
Join Date: Sep 2008
Posts: 30
Reputation:
Solved Threads: 1
Here is what helped me if anyone else is having this same problem:
C++ Syntax (Toggle Plain Text)
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++; }
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: global variable
- Next Thread: [0-9] combination Problem
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int integer java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






