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;
}

combine the loops on lines 31 and 37 because there should only be one loop, not two.

while( infile >> lname >> fname >> midInitial )
{
    file2 = fname + lname + ".dat";
    ifstream in(file2.c_str());
    if( in.is_open())
    {
         while( in >> ton )
         {
              // do stuff here
         }
    }
}

i dont understand this part of what you said here:

if( in.is_open())
{

}

here is my improved code still need help though..any advice is appreciated:

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

using namespace std;

int main()
{
	int large, small, ton, iTon, oTon, iCount, oCount;
	string lname, fname, midInitial;
	string filename, file2;
	ifstream infile, inData;

	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";
		}

	while(infile >> lname >> fname >> midInitial)
	{
	cout << lname << " " << fname << " " << midInitial << "\n";

		file2 = fname + lname + ".dat";

		inData.open(file2.c_str());	
		if(!infile)					
		{
			cout << "\nError... could not find file...\n";
		}

		while(inData >> ton)     //think there is a problem here
		{
			cout << ton;
		
			iTon += ton;
			iCount++;
			oTon += ton;
			oCount++;
		}

		while(ton > large)
		{
			large = ton;
		}

		while(ton < small)
		{
			small = ton;
		}
	}	

	cout << "\n" << "Ton: " << ton << fixed << setprecision(2);
	cout << "Min: " << small << " " << "Max: " << 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();
	inData.close();
	inData.clear();

		return 0;
}

here is my improved code still need help though.

You do? I don't see any description about what you need help with. Are we just supposed to guess?

.any advice is appreciated:

Always explain what you need, and why. Never leave it to us to guess. Mostly we guess wrong and it wastes both our times.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.