// Solution of Assignment 9
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
	ifstream inFile;
	ofstream outFile;
	string inFileName, outFileName;
	float exam1, exam2, exam3, avg;
	string line;
	
	// Read the path and the name of the input file
	cout << "Enter the path and the name of the input file: ";
	cin >> inFileName;

	// Try to open the input file
	inFile.open(inFileName.c_str());
	if (!inFile) // If openning the file fails
	{
		cout << "Can't open file: " << inFileName << ".\n";
		exit(1); // Quit
	}
	
	// Read the path and the name of the output file
	cout << "Enter the path and the name of the output file: ";
	cin >> outFileName;

	// Try to open the output file
	outFile.open(outFileName.c_str());
	if (!outFile) // If openning the file fails
	{
		cout << "Can't open file: " << outFileName << ".\n";
		exit(1); // Quit
	}

	// Read the first line of the input file.
	getline(inFile,line);
	// Write it to the output file.
	outFile << line << endl;
	// Write also the following information
	outFile << "Exam1" << '\t' << "Exam2" << '\t'
			<< "Exam3" << '\t' << "Average" << ".\n";
	// Try to read the grades of the first student in the input file.
	inFile >> exam1 >> exam2 >> exam3;
	while (inFile) // while reading from file succeeds
	{
		// Calculate the average
		avg = (exam1 + exam2 + exam3)/3;
		// Write the grades and their average to output file
		outFile << exam1 << '\t' << exam2 << '\t'
				<< exam3 << '\t' << avg << '\n';
		// Try to read the grades of the next student
		inFile >> exam1 >> exam2 >> exam3;
	}
	
	// Close the files
	inFile.close();
	outFile.close();
	return 0;
}
