ASDV-C-Sharp/MP5/DriversLicenseExam_CalebFontenot/Form1.cs
2022-11-27 17:11:02 -06:00

85 lines
2.8 KiB
C#
Executable File

namespace DriversLicenseExam_CalebFontenot
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void exitButton_Click(object sender, EventArgs e)
{
this.Close();
}
private void openFileButton_Click(object sender, EventArgs e)
{
// Create variables
string[] correctAnswers = new string[20],
studentAnswers = new string[20];
bool[] correctAnswerCompare = new bool[20];
// Read the file with the correct answers.
correctAnswers = readFiles("correctAnswers.txt");
// Now read the file with the student answers.
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
studentAnswers = readFiles(openFileDialog1.FileName);
}
if (correctAnswers != null & studentAnswers != null)
{
for (int i = 0; i < correctAnswers.Length; i++)
{
if (correctAnswers[i] == studentAnswers[i])
{
correctAnswerCompare[i] = true;
}
else
{
correctAnswerCompare[i] = false;
}
}
// Count up the correct answers
int numCorrect = 0;
for (int i = 0; i < correctAnswerCompare.Length; i++)
{
if (correctAnswerCompare[i] == true)
{
numCorrect++;
}
}
correctAnswersLabel.Text = numCorrect.ToString() + "/20";
double gradePercentage = (numCorrect / 20.0);
feedbackLabel.Text = "You scored a " + gradePercentage.ToString("P");
} else
{
MessageBox.Show("Unable to open file(s).");
}
}
private string[] readFiles(string fileName)
{
try
{
// Create StreamReader to read the correct answers.
StreamReader inputFile = File.OpenText(fileName);
string[] returnStringArray = new string[20];
int i = 0;
while (!inputFile.EndOfStream)
{
returnStringArray[i] = inputFile.ReadLine();
i++;
}
inputFile.Close();
return returnStringArray;
} catch
{
return null;
}
}
}
}