/* Lab 6 assignment: while loops: counting, sentinel, end-of-file
---------------------------------------------
Patrick Schmid pds2
February 6, 2005
Lab solution
Lab instructor: Patrick Schmid
Lab section: 10 & 11
Purpose: Calculate average weight of each group of football players.
		 Groups: backs (1-49), lineman (50-79), other (80-99).
		 Given:
		 - table with 2 columns, 20 rows
		 - First column player number, second column weight
		 Use a while loop
Algorithm: end-of-file while loop. Read each row, use if-else if-else
		   to determine what kind of player. Increase appropriate count
		   and add weight to weight sum of player group. Calculate averages
		   after all rows have been read while outputting results.
*/

#include <fstream.h>
#include <iomanip.h>

void main() {
	//declare variables
	int count; //count for all players
	int countBacks; //count for backs
	int countLineman; //count for lineman
	int countOthers; //count for others
	double sumBacks; //weight sum for backs
	double sumLineman; //weight sum for lineman
	double sumOthers; //weight sum for others
	int number; //temporary variable to read player number from file
	double weight; //temporary variable to read player weight from file

	//open files
	ifstream in("lufootballstats.txt", ios::in);
	ofstream out("weightresults.txt", ios::out);

	//initialize all counts and all sums to 0
	count=countBacks=countLineman=countOthers=0;
	sumBacks=sumLineman=sumOthers=0;

	//echo print while reading
	//tell user that you are going to read player stats from file
	cout<<"Reading player stats from file...\n\n";	
	out<<"Reading player stats from file...\n\n";

	//table header for echo printing of player stats
	cout<<"Player number | Weight\n";
	cout<<"----------------------\n";
	out<<"Player number | Weight\n";
	out<<"----------------------\n";

	//read first row from file
	//IMPORTANT: This needs to be right before the while() loop
	in>>number>>weight;

	while (in.good()) {  //in.good() is the same as !in.fail()
	
		//we read one more player, hence increase player count by one
		count++; 
		
		//echo print player stats in table
		cout<<setw(13)<<number<<" | "<<setw(6)<<weight<<endl;
		out<<setw(13)<<number<<" | "<<setw(6)<<weight<<endl;

		//determine what type of player we just read
		//increase appropriate counter and add to appropriate weight
		if (number>=1 && number<=49) { //back
			countBacks++;
			sumBacks+=weight;
		}
		else if (number>=50 && number<=79) { //lineman
			countLineman++;
			sumLineman+=weight;
		}
		else { //others
			countOthers++;
			sumOthers+=weight;
		}

		//read next next from file
		//IMPORTANT: This needs to be right before the } of the while loop
		in>>number>>weight;
	}

	//output results to screen
	cout<<endl<<"Number of players in total: "<<count<<endl;
	cout<<"Average weight of all players: "<<(sumBacks+sumLineman+sumOthers)/count<<endl;

	cout<<"Number of backs: "<<countBacks<<endl;
	cout<<"Average weight of all backs: "<<sumBacks/countBacks<<endl;
	
	cout<<"Number of lineman: "<<countLineman<<endl;
	cout<<"Average weight of all lineman: "<<sumLineman/countLineman<<endl;

	cout<<"Number of others: "<<countOthers<<endl;
	cout<<"Average weight of all others: "<<sumOthers/countOthers<<endl;

	//output results to file
	out<<endl<<"Number of players in total: "<<count<<endl;
	out<<"Average weight of all players: "<<(sumBacks+sumLineman+sumOthers)/count<<endl;

	out<<"Number of backs: "<<countBacks<<endl;
	out<<"Average weight of all backs: "<<sumBacks/countBacks<<endl;
	
	out<<"Number of lineman: "<<countLineman<<endl;
	out<<"Average weight of all lineman: "<<sumLineman/countLineman<<endl;

	out<<"Number of others: "<<countOthers<<endl;
	out<<"Average weight of all others: "<<sumOthers/countOthers<<endl;

	//close files
	in.close();
	out.close();
}
