/*Write a C++ program with the following features:
1. Function main asks the user to enter a name of a file and then it reads the name of the file.
2. The file mentioned should have the following contents. The first line is  
	(the contents must be in the given format):
			SSN	Exam 1		Exam 2		A1		A2		A3
        Then each following line will contain the record of one student. The last 4 digits 
		of his/her SSN must be listed under SSN. His/her grade in Exam1 must be listed under 
		Exam1. Exam1 is out of 100. His/her grade in Exam2 must be listed under Exam2. 
		Exam2 is out of 100. His/her grade in Assignment 1 must be listed under A1. 
		A1 is out of 50. His/her grade in Assignment 2 must be listed under A2. 
		A2 is out of 50. His/her grade in Assignment 3 must be listed under A3. 
		A3 is out of 50. For example, assume that John has a record in that file and 
		assume that the last 4 digits of his SSN are 4306 and assume his grade in Exam1 is 85, 
		in Exam2 is 98, in Assignment1 is 40, in Assignment2 is 45, and in Assignment3 is 49. 
		Then the following line must be in the file:
			4306	85		98		40		45		49
		Assume the grades are real-valued (i.e. they may contain fractional parts). 
3. Then function main calls a void function called CSIT121 and passes to it the name of the 
	file entered by the user. This means the only input parameter to CSIT121 should be the name 
	of the file.
4. Function CSIT121 must do the following:
(a) It opens the file whose name was entered by the user for reading.
(b) It opens another file called Grades.txt for writing.
(c) On the first line of file Grades.txt, function CSIT121 should write the following
	SSN		Course grade
(d) Then function CSIT121 calculates the numeric course grade of each student and writes to a 
	separate line of file Grades.txt, the last 4 digits of his/her SSN, followed by his/her 
	numeric course grade. When you calculate the numeric course grade, assume each exam consists 
	30% of the course grade and the assignments (all together) consist 40% of the course grade 
	(assume the assignments have the same weight). The numeric course grade should be out 100 
	and should have zero decimal places. So, you should declare numeric course grade to be of 
	type float and then write it to file Grades.txt with 0 decimal places. For example, if the 
	student's numeric course grade is 79.6, then his/her numeric course grade must be recorded 
	on file Grades.txt as 80. Each line of file Grades.txt must contain the last 4 digits of 
	the SSN of one student and his/her numeric course grade, listed in the order shown in the 
	first line of file Grades.txt. I.e. the last 4 digits of the SSN come first, then the 
	numeric course grade.
(e) Function CSIT121 calculates the minimum numeric course grade in the class and the maximum 
	numeric course grade and the average (of numeric course grades) of class. Each one of the 
	previous ones should be of type float. 
(f) Then function CSIT121 writes (should be the last line of file Grades.txt) to file Grades.txt, 
	 the minimum numeric course grade with zero decimal places, the maximum numeric course grade 
	 with zero decimal places, and the average with zero decimal places. For example, if the 
	 minimum numeric course grade is 67.523334 and the maximum numeric course grade is 97.473089,
	 and the average is 83.821874, then the last line of Grades.txt should be as follows:
		Minimum: 68. Maximum: 97. Average: 84.
(g) Then function CSIT121 returns the minimum numeric course grade, the maximum course numeric 
	grade, and the average to function main.
5. Then function main displays the minimum numeric course grade, the maximum numeric course 
	grade, and the average as they were written on Grades.txt. Using the example above, what 
	should be displayed by function main is:
		Minimum: 68. Maximum: 97. Average: 84.
6. There should be no global variables in your program and function CSIT121 should be of type 
	void.
7. File opening, closing, and processing and also all calculations must be done in function 
	CSIT121.
8. Function main should not do file processing at all and it should do no calculations. It 
	should only read the name of the input file, calls function CSIT121, and display the 3 
	values returned by function CSIT121.
Example:
Suppose that the contents of file Test.dat are as follows:
SSN     Exam1   Exam2   A1      A2      A3
1234    	56      	77        30       40       50
4321      45           77        44       43       34
6778      56           88        43       41       45
Here's a sample run:
Enter the name of the input file: Test.dat
Minimum: 69. Maximum: 78. Average: 73.
Press any key to continue
The contents of file Grades.txt are now 
SSN                  Course Grade
1234                 	    72
4321                 	    69
6778                 	    78
Minimum: 69. Maximum: 78. Average: 73.

Solution:
*/

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
void CSIT121(string FileName,float& Min,float& Max, float& Average);

int main()
{
	float Min, Max, Average;
	string FileName;
	cout << "Enter the name of the input file: ";
	cin >> FileName;
	CSIT121(FileName,Min,Max,Average);
	cout << "Minimum: " << fixed << setprecision(0) << Min 
		 << ". ";
	cout << "Maximum: " << fixed << setprecision(0) << Max
		 << ". ";
	cout << "Average: " << fixed << setprecision(0) << Average 
		 << "."
		 << endl;
	return 0;
}

void CSIT121(string FileName,float& Min,float& Max, float& Average)
{
	string St1,St2,St3,St4,St5,St6;
	ifstream inFile;
	ofstream outFile;
	int SSN, Count = 0;
	float Numeric, Exam1, Exam2, A1, A2, A3;
	inFile.open(FileName.c_str());
	if ( !inFile )
	{
		cout << "Can't open the file" << endl;
		exit(1);
	}
	Average = 0;
	Min = 1000;
	Max = -1;
	outFile.open("Grades.txt");
	outFile << "SSN" 
			<< setw(30) << "Course Grade"
			<< endl;
	inFile >> St1 >> St2 >> St3 >> St4 >> St5 >> St6;
	inFile >> SSN >> Exam1 >> Exam2 >> A1 >> A2 >> A3;
	/*cout << SSN << " " << Exam1 <<" " << Exam2 << " "
		<< A1 << " " << A2 << " " << A3 << endl; */
	while ( inFile )
	{
		Count++;
		Numeric = 0;
		Numeric = Numeric + 0.3 * (Exam1 + Exam2) + (4.0/15.0)*(A1+A2+A3);
		outFile << SSN 
				<< setw(30) << fixed << setprecision(0) << Numeric
				<< endl;
		Average = Average + Numeric;
		if (Numeric > Max)
			Max = Numeric;
		if (Numeric < Min)
			Min = Numeric;
		inFile >> SSN >> Exam1 >> Exam2 >> A1 >> A2 >> A3;
	}
	Average = Average/Count;
	outFile << "Minimum: " << fixed << setprecision(0) << Min 
		 << ". ";
	outFile << "Maximum: " << fixed << setprecision(0) << Max
		 << ". ";
	outFile << "Average: " << fixed << setprecision(0) << Average 
		 << "."
		 << endl; 
	inFile.close();
	outFile.close();
}
