//  Author: Brandon Mohawk
//  Teacher:  Dr. Wang
//  Due Date: 02/12/07
//
//  File: examgrader.cpp
//  Compile:  g++ examgrader.cpp -o examgrader.out
//  Run:  ./examgrader.cpp
//
//  Goal: The program will read two text files consisting
//  of a list of answers, CorrectAnswers.txt being the
//  the correct answers, and StudentAnswers.txt being the
//  student's answers.  The program then displays how many
//  questions the student got wrong, which questions he got
//  wrong and his answer, what % of the questions he got
//  correct, and whether or not he passed the exam.
#include <iostream>
#include <fstream>

using namespace std;

const int M = 20;
ifstream inStud, inCor;

void compareTo(char[], char[]);

int main()
{

	system("clear");
	
	char correct[M], student[M];

	inStud.open("StudentAnswers.txt");
	inCor.open("CorrectAnswers.txt");

	if (!inStud)
	{
		cout << "StudentAnswers.txt was not found.\n";
		exit(0);
	}

	for (int i=0; i<M; i++)
		inStud >> student[i];

	if (!inCor)
	{
		cout << "CorrectAnswers.txt was not found.\n";
		exit(0);
	}

	for (int i=0; i<M; i++)
		inCor >> correct[i];
	
	compareTo(correct, student);

	return 0;
}

void compareTo(char correct[], char student[])
{
	cout << "The student missed the following questions:\n\n";

	int num = 0;
	for (int i=0; i<M; i++)
	{
		if (student[i] != correct[i])
		{
			num++;
			 if (i < 10)
                        {
                                cout << "Question  " << i+1;
				cout << ".  Correct answer: ";
				cout << correct[i];
                                cout << ".  Student's answer: ";
				cout << student[i] << ".\n";
                        }

                        else
                        {
                                cout << "Question " << i+1;
				cout << ".  Correct answer: ";
				cout << correct[i];
                                cout << ".  Student's answer: ";
				cout << student[i] << ".\n";
                        }
		}
	}

	float percent = (((float(M)) - num) / M) * 100;

	cout << "\n";
	cout << "The studen got " << num << " questions wrong.\n";
	cout << "The student got " << percent << "% of the questions";
	cout << "correct.\n";
        
	if (percent >= 70)
                cout << "The student passed the exam!\n\n";
        else
                cout << "The student failed the exam.\n\n";
}

