More help with 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.

Input file is a .txt file with this in it:
bccbbadbca
bccbbadbca
bccdbadbca
accdbadbca

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
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int gradeExam(string key, string exam);

int main()
{
   string key;
   string exam;
   int studentGrade;

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

   infile >> key >> exam;

   while (infile >> exam)
   {
      gradeExam(key, exam);
      cout << "Student " << exam << " " << studentGrade << endl;
   }



   return 0;
}

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

   for (int i = 0; i < 10; i++)
   {
      if (key[i] == exam[i])
         studentGrade = studentGrade + 1;
      else
    	 studentGrade += 0;
   }


   return 0;
}


This is the output:

Student bccdbadbca 2665468
Student accdbadbca 2665468


I want it to say "Student [index #] - [grade]", where the "index #" is which student's exam it was and "grade" is what they scored on the exam.
I want it to say "Student [index #] - [grade]",
So use his index # and not his exam string. You would need to create variable to hold current student number and increment it in loop:
1
2
3
4
int index = 0;
while (infile >> exam)  {
    ++index;
//... 

I want it to say "Student [index #] - [grade]",
There is several errors:
1) You calculate student grade in gradeExam function... and discard it. It does not leave the function. You should return it instead of 0.
2) Even if it would return valid number, you do not use it in main(); value returned by your function just gets discarded. Assign it to a variable you created for it. (right now you are using it uninitialised.)
Last edited on
Okay so I did this:

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
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int gradeExam(string key, string exam);

int main()
{
   string key;
   string exam;

   int studentGrade;

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

   infile >> key >> exam;

   int index = 0;

   while (infile >> exam)
   {
      ++index;
	  studentGrade = gradeExam(key, exam);
      cout << "Student " << index << " - " << studentGrade << endl;
   }



   return 0;
}

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

   for (int i = 0; i < 10; i++)
   {
      if (key[i] == exam[i])
         studentGrade = studentGrade + 1;
	  else
	     studentGrade += 0;
	   }


   return studentGrade;
}


and it worked like a charm.

Thanks!
Topic archived. No new replies allowed.