So I'm currently working on a grading program and I am to write a function that grades ANexam and returns the score when passed the key and and one set of answers. The answers to the exam cannot be stored and I am to only use an array to output how many earned each score (out of a possible 11).
This other function will take that array and # of questions as parameters and find the high, low, and mean.
However, I am completely unsure after hours of digging on just how to compare each element of the answer key to each element of the students answers.
The .txt I'd pull from would be structured as follows:
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
constint MAX_STUDENTS = 250;
const string sGrades = "grade_data.txt";
bool getData(string gFile, string grades[],int &numGrades);
int scoreAnswers(string grades[], int numGrades); // Used later
int main ()
{
string grades[MAX_STUDENTS];
int numGrades = 0;
getData(sGrades, grades, numGrades);
cout << "Total Grades Read in: " << numGrades <<endl;
for (int i = 0; i < numGrades; i++)
cout << i << " " << grades[i]<< endl;
cout << "# of Correct answers " << grades[scoreAnswers(grades, numGrades)]
<< endl;
system("pause");
return 0;
}
int scoreAnswers(string grades[], int numGrades)
{
string ansKey (grades[0]);
int score = 0;
for (int i=1; i < numGrades; i++)
score +=(ansKey == grades[i]);
//if (ansKey.compare(grades[i]) == 0)
//cout << ansKey << " this is right " << i << " = 0 " << endl;
cout << "Total Correct " << score << endl;
// if (ansKey.come(grades[i])
return score;
}
bool getData(string gFile, string grades[], int &numGrades)
{
ifstream inFile;
inFile.open(sGrades.c_str());
if (!inFile) //Used later in an unwritted function
returnfalse;
string answers;
inFile >> answers; // 1st value read in @0
while (!inFile.eof()) // Reads values after 0
{
grades[numGrades] = answers;
numGrades++;
inFile >> answers;
}
returntrue;
}
When ran, I am given a number that reflects how many strings equally match the answer key. How would I proceed to compare each element of the answer key to each element of the students answers and return the number of elements matching that of the answer key?