Exam Grader

Hey, everyone.

I was wondering if anyone could help me out with this program. Basically, I have to write a code that will grade an exam, which is a set of strings in a file with the first line being the answer key, and the following lines being the student's exams.

"The program will grade a series of exams and then print a grade report for students in a course.

Input: An instructor has a class of students each of whom takes a multiple-choice exam with 10 questions. For each student in the class, there is one line in the input file. The line contains the answers that student gave for the exam. The input file named "grade_data.txt" will have the following format:

line 1: the key for the exam (e.g.)

bccbbadbca

lines 2-n:

a set of answers. You know you are done when you get to a line with no data.
e.g. bccdbadda

Note: You will not know in advance how many exams you have to grade and you don't need to store the exam answers in your program."

This is what I have so far and it's not getting me anywhere. PLEASE HELP.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <fstream>

using namespace std;

int gradeExam(string key, string exam);

int main()
{
   string key;
   string exam;

   ifstream infile;
   infile.open ("grade_data.txt");

   infile >> key;
   
   int gradeExam(string key, string exam);

   return 0;
}

int gradeExam(string key, string exam)
{
   int i = 0, grade;
   grade = 0;

   while (exam != " ")
   {
   if (key[i] == exam[i])
      grade = grade + 1;
      cout << "Student " << exam[i] << " - " << grade << endl;
      i++;
   }

   return 0; //(instead of returning 0, should I return "grade"?)
}
1) If you want to get grade from your function, you should indeed return it.
2) Line 18 is identical to line 6. It is not a function call
3) You do not read exam string from your file.

4) In your grade function you do not need to output grade.
5) In grade function you should fix your while loop condition (or change it to for loop which is way better here)

To loop on input lines there is good idiom:
1
2
3
4
infile >> key;
while(infile >> exam) {
    //grade exam, output something, etc.
}


Also if grade is just a number of pairwise matches, standard library have function to do exactly that:
1
2
3
4
5
6
7
8
#include <numeric>
#include <functional>
//...
int gradeExam(std::string key, std::string exam)
{
    return std::inner_product(key.begin(), key.end(), exam.begin(), 0,
                              std::plus<int>(), std::equal_to<int>());
}
Topic archived. No new replies allowed.