/*
The following program reads the name of an input file with the following 
structure: the first line of the file contains information and every 
other line contains a SSN of a student (of type string with no space 
withing the SSN) followed by the grade of that student (the grade is of 
type float and it's separated from the SSN by space or tabs). The program 
finds the number of students in the file, the average of the grades, and 
the heighest grade, and it prints that to both the screen and an output 
file whose name is entered by the user. 
*/
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
	string fileName, line, ssn;
	float sum = 0, grade, max = -100;
	int count = 0;
	ifstream inFile;
	cout << "Enter the input file name: ";
	cin >> fileName;
	inFile.open(fileName.c_str());
	if (!inFile)
	{
		cout << "can't open file." << endl;
		return 1;
	}
	getline(inFile,line); // read 1st line
	inFile >> ssn >> grade;
	while (inFile)
	{
		sum += grade;
		count++;
		if (grade > max)
			max = grade;
		inFile >> ssn >> grade;
	}
	inFile.close();
	cout << "There are " << count << " grades.\n";
	cout << "The average is " << sum / count << ".\n";
	cout << "The heighest grade is " << max << ".\n";
	
	ofstream outFile;
	cout << "Enter the output file name:";
	cin >> fileName;
	outFile.open(fileName.c_str());
	if (!outFile)
	{
		cout << "can't open file." << endl;
		return 1;
	}
	outFile  << "There are " << count << " grades.\n";
	outFile  << "The average is " << sum / count << ".\n";
	outFile  << "The heighest grade is " << max << ".\n";
	outFile.close();
	return 0;
}

