Exam Grader

This is part of a class assignment. I can't get in touch with my teacher, so I have nowhere else to turn.

There are two text files with this program, an answer key and a test "student" file for test, but it spits out the wrong results (For example, it says the second question was wrong, when in fact, it's correct.)

What am I doing wrong?

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;

void missed(char, char, int, int&);
void passOrFail(float);

int main()
{
	ifstream correctFile;
	ifstream studentFile;
	const int MAX = 20;
	char answers[MAX];
	char student[MAX];
	int miss = 0;	//Number of missed questions
	float grade;	//Student's grade
	
	correctFile.open("CorrectAnswers.txt");
	cout << "Now opening Correct Answers file..." << endl;

	while (!correctFile.eof())
	{
		for (int i = 0; i <= MAX; i++)
		{
			answers[i] = correctFile.get();
		}
	}

	studentFile.open("StudentAnswers.txt");
	cout << "Now opening Student Answers file..." << endl;

	while (!studentFile.eof())
	{
		for (int j = 0; j <= MAX; j++)
		{
			student[j] = studentFile.get();
		}
	}

	correctFile.close();
	studentFile.close();

	for (int k = 0; k <= MAX; k++)
	{
		missed(student[k], answers[k], k, miss);
	}

	cout << "You missed " << miss << " questions." << endl;
	
	grade = (20 - miss) / 20;

	passOrFail(grade);

	system("pause");
	return 0;
}

void missed(char s, char a, int l, int &wrong)
{
	if (s != a)
	{
		cout << "For Question " << l + 1 << " you answered " << endl;
		cout << s << ". The correct answer was " << a << "." << endl;
		wrong += 1;
	}
}

void passOrFail(float g)
{
	cout << "Your grade is " << g << endl;
}


CorrectAnswers.txt
A
D
B
C
A
A
D
B
D
C
A
D
B
A
C
C
D
B
C
A

Student Answers
A
D
B
D
A
A
D
B
D
C
A
A
B
A
C
C
C
B
C
A


Any help is appreciated...
1
2
3
4
5
6
7
	while (!correctFile.eof())
	{
		for (int i = 0; i <= MAX; i++)
		{
			answers[i] = correctFile.get();
		}
	}
I would do something like
1
2
3
4
5
6
for(int i = 0; correctFile && i <MAX; ++i)
{
    int temp;
    correctFile >> temp;
    answer[i] = temp;
}
EOF means end of file there are other errors that should stop reading. Also If an array is the size of MAX then it starts at 0 and ends at MAX - 1 so you would read while it is less than MAX.


Though I would suggest something like this:

1
2
3
4
5
6
7
8
9
10
11
#include <vector>

..

std::vector<char> answers;

int temp;
while(correctFile >> temp)
{
    answers.push_back(answers);
}

The reason you can use the std::ifstream object to check against error states like this is because they overloaded operator bool http://www.cplusplus.com/reference/ios/ios/operator_bool/


You have a slight prblem here grade = (20 - miss) / 20; this is what we call integer division. Say for example miss is 15 then 20 - 15 = 5. 5 / 20 = 0 (since integer if it was float it would be .25).

So change it to something like 20., 20f, 20.f, 20.0 or 20.0f

Oh and instead of calling the open method on your std::ifstream's you can use the constructor. something like: std::ifstream in("text.txt"); instead of
1
2
std::ifstream in;
in.open("text.txt");
Last edited on
Thanks. I tried the non-vector method, and it doesn't read anything at all.

I'm incredibly rusty at C++. We haven't even really covered vectors yet.
Thanks. I tried the non-vector method, and it doesn't read anything at all.
Would you please show the code you tried?
1
2
3
4
5
for(int i = 0; correctFile && i < MAX; ++i)
	{
		correctFile >> temp;
		answers[i] = temp;
	}
I mean the complete code. (compileable)
Last edited on
Sorry... head's kind of fuzzy.

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;

void missed(char, char, int, int&);
void passOrFail(float);

int main()
{
	ifstream correctFile;
	ifstream studentFile;
	const int MAX = 20;
	char answers[MAX];
	char student[MAX];
	int miss = 0;	//Number of missed questions
	int temp;
	float grade;	//Student's grade
	
	correctFile.open("CorrectAnswers.txt");
	cout << "Now opening Correct Answers file..." << endl;

	for(int i = 0; correctFile && i < MAX; ++i)
	{
		correctFile >> temp;
		answers[i] = temp;
	}


	studentFile.open("StudentAnswers.txt");
	cout << "Now opening Student Answers file..." << endl;

	for(int j = 0; studentFile && j < MAX; ++j)
	{
		studentFile >> temp;
		answers[j] = temp;
	}


	correctFile.close();
	studentFile.close();

	for (int k = 0; k < MAX; k++)
	{
		missed(student[k], answers[k], k, miss);
	}

	cout << "You missed " << miss << " questions." << endl;
	
	grade = (20.0 - miss) / 20.0;

	passOrFail(grade);

	system("pause");
	return 0;
}

void missed(char s, char a, int l, int &wrong)
{
	if (s != a)
	{
		cout << "For Question " << l + 1 << " you answered " << endl;
		cout << s << ". The correct answer was " << a << "." << endl;
		wrong += 1;
	}
}

void passOrFail(float g)
{
	cout << "Your grade is " << g << endl;
}
Sorry, int temp; should be char temp; forgot you were using a-d instead of 1-4. Though you are reading into answers twice instead of answers and student. so line 37 should be student[i] = temp;

With your sample files I get an output of
Now opening Correct Answers file...
Now opening Student Answers file...
For Question 4 you answered
D. The correct answer was C.
For Question 12 you answered
A. The correct answer was D.
For Question 17 you answered
C. The correct answer was D.
You missed 3 questions.
Your grade is 0.85

Process returned 0 (0x0)   execution time : 0.261 s
Press any key to continue.
Last edited on
Awesome. giblit, thank you SO much! Still a few more touches I need to put before turning it in, but you got me through a block. Thank you!
Topic archived. No new replies allowed.