I have an assignment 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
The 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.
Im still relatively new to programming so I don't really know where i went wrong i marked where i think the code is messing up and any other pointers you could give would be much appreciated...thank you.. here is the code:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
int large, small, ton, iTon, oTon, iCount, oCount;
string lname, fname, midInitial;
string filename, file2;
ifstream infile;
large = 0; //for determining max tonnage
small = 999; //for determining min tonnage
ton = 0;
iTon = 0; //idividual Tonnage
iCount = 0; //idividual count or number of seasons
oTon = 0; //overall tonnage
oCount = 0; //overall count of number of seasons
cout << "Enter filename: ";
cin >> filename;
infile.open(filename.c_str());
if(!infile) //test if file opens
{
cout << "\nError... could not find file...\n";
}
do
{
infile >> lname >> fname >> midInitial;
cout << lname << " " << fname << " " << midInitial << "\n";
}while(!infile.eof());
do
{
file2 = fname + lname + ".dat";
infile.open(file2.c_str()); //having trouble finding this file....is there something wrong with its declaration?
if(!infile)
{
cout << "\nError... could not find file...\n";
}
infile >> ton;
cout << ton;
iTon += ton;
iCount++;
oTon += ton;
oCount++;
}while(!infile.eof());
while(ton > large)
{
large = ton;
}
while(ton < small)
{
small = ton;
}
cout << "\n" << fixed << setprecision(2) << ton;
cout << small << " " << large;
double avg = iTon/iCount; //to find idividual Avg tonnage
double oAvg = oTon/oCount; //to find overall Avg tonnage
cout << "\n" << avg << "\n" << oAvg;
infile.close();
infile.clear();
return 0;
}