testgrader. cannot compile

im getting an error can anyone please inform me on what im doing wrong. i am a bit new to c++. anything would help. thanks in advance.

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
  #include <iostream>
#include <string>
#include <cctype>
using namespace std;


class TestGrader
{
private:
	static const int SIZE = 20;
	int sum;
	int wrong;
	int count;

public:	
	TestGrader();
	void setKey(char[]);
	int grade(char[], char[]);
};

TestGrader::TestGrader()
{}

void TestGrader::setKey(char answers[])
{
	for (count = 0; count < SIZE; count++)
	{
		cout << "Please enter the correct answer for question " << (count + 1) << ": ";
		cin >> answers[count];
	}
}
int TestGrader::grade(char answers[], char student[])
{
	int sum = 0;

	for (count = 0; count < SIZE; count++)
	{
		if (answers[count] != student[count])
		{
			cout << endl;
			cout << "Answer " << (count + 1) << " is wrong.\n";
		}
		else
		{
			sum += 1;
		}
	}
	
	return sum;
}

int main()
{
	TestGrader test;
	const int SIZE = 20;
	int wrong, index;
	char answers[SIZE];
	char student[SIZE];
	char continuegrade;
	for(int index = 0; index < 20; index++)
	{
		cout << "Driver's License Program Exam Grades";
		cout << "USING ONLY CAPITAL A, B, C, or D AS ANSWERS" << endl;
		cout << "Answer for question " << (index + 1) << " " << endl;
        cin >> answers;
        student[index] = answers;
        while(student[index]!= 'A'
			&&student[index] != 'B'
			&&student[index] != 'C'
			&&student[index] != 'D')
			{
				cout << "!! ERROR !! PLEASE USE A,B,C OR D TO ANSWER QUESTION(S)" << endl;
			    cin >> answers[index];  
			}
			test.grade(answers);
			cout << "Do you want to grade another student? ";
			cin >> continuegrade;
	}
	test.setKey(answers);
	int sum = test.grade(answers, student);
	wrong = SIZE - sum;
	cout << "Student got " << sum << " right. " << endl;
	cout << "Student got " << wrong << " wrong. " << endl;

	if (sum >= 15)
	{
		cout << "Student passed " << endl;
	}

	else
	{
		cout << "Student failed " << endl;
	}
	return 0;

}
Last edited on
i'm a bit confused as to why its not compiling

Perhaps because there are errors in your code?

Please post the error messages your compiler is generating then maybe someone can help you decipher your error messages.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
C:\Users\brittany\Documents\PROG. FUNDAMENTALS\ch8exam.cpp	In function 'int main()':
26	24	C:\Users\brittany\Documents\PROG. FUNDAMENTALS\ch8exam.cpp	[Error] no matching function for call to 'testgrader::setKey(char [20])'
26	24	C:\Users\brittany\Documents\PROG. FUNDAMENTALS\ch8exam.cpp	[Note] candidate is:
11	11	C:\Users\brittany\Documents\PROG. FUNDAMENTALS\ch8exam.cpp	[Note] void testgrader::setKey(char*, int)
11	11	C:\Users\brittany\Documents\PROG. FUNDAMENTALS\ch8exam.cpp	[Note] candidate expects 2 arguments, 1 provided
45	15	C:\Users\brittany\Documents\PROG. FUNDAMENTALS\ch8exam.cpp	[Error] 'rightanswers' was not declared in this scope
47	32	C:\Users\brittany\Documents\PROG. FUNDAMENTALS\ch8exam.cpp	[Error] no matching function for call to 'testgrader::grade(char [20])'
47	32	C:\Users\brittany\Documents\PROG. FUNDAMENTALS\ch8exam.cpp	[Note] candidate is:
12	8	C:\Users\brittany\Documents\PROG. FUNDAMENTALS\ch8exam.cpp	[Note] void testgrader::grade(char*, char*, int, int)
12	8	C:\Users\brittany\Documents\PROG. FUNDAMENTALS\ch8exam.cpp	[Note] candidate expects 4 arguments, 1 provided
C:\Users\brittany\Documents\PROG. FUNDAMENTALS\ch8exam.cpp	At global scope:
56	6	C:\Users\brittany\Documents\PROG. FUNDAMENTALS\ch8exam.cpp	[Error] prototype for 'void testgrader::setKey(char*)' does not match any in class 'testgrader'
11	11	C:\Users\brittany\Documents\PROG. FUNDAMENTALS\ch8exam.cpp	[Error] candidate is: void testgrader::setKey(char*, int)
C:\Users\brittany\Documents\PROG. FUNDAMENTALS\ch8exam.cpp	In member function 'void testgrader::grade(char*, char*, int, int)':
72	25	C:\Users\brittany\Documents\PROG. FUNDAMENTALS\ch8exam.cpp	[Error] 'studntanswers1' was not declared in this scope
Okay, what exactly don't you understand about that first error message? And also note that the next two messages are part of that first message. And the problem was first detected on line 26.
oh i just realized i had combined the code with another, im sure ill still have some errors let me clear up the code.
these are the errors
1
2
3
4
5
6
In function 'int main()':
66	24	[Error] invalid conversion from 'char*' to 'char' [-fpermissive]
75	22	[Error] no matching function for call to 'TestGrader::grade(char [20])'
75	22	[Note] candidate is:
32	5	[Note] int TestGrader::grade(char*, char*)
32	5	[Note] candidate expects 2 arguments, 1 provided
Last edited on
 
student[index] = answers;


Something isn't right there. What is it? They're both character arrays. We can't make that assignment.

The definition for grade is:
 
int grade(char[], char[]);

That definition says it takes two character arrays as its arguments.

You only passed it one character array.
 
test.grade(answers);
okay so i cleaned up the code, i got it to run but im having issues with the result. im supposed to grade a student and if he/she gets 15 points right then its a pass but anything less than 15 is failed. what am i doing wrong in the code that prevents the main function from grading. thanks in advance.
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
class TestGrader
{
private:
	static const int SIZE = 20;
	int sum;
	int wrong;
	int count;

public:	
	TestGrader();
	void setKey(char[]);
	int grade(char[], char[]);
};

TestGrader::TestGrader()
{}

void TestGrader::setKey(char answers[])
{
	for(count = 0; count < 20; count++)
	{
		cout << "Enter answer for question " << (count + 1) << ": " << endl;
		cin >> answers[count];
		while(answers[count]!= 'A'
			&&answers[count] != 'B'
			&&answers[count] != 'C'
			&&answers[count] != 'D')
			{
				cout << "MUST use CAPITAL A,B,C OR D to answer question(s)" << endl;
			    cin >> answers[count];  
			}
	}
}
int TestGrader::grade(char answers[], char student[])
{
	int sum = 0;

	for (count = 0; count < SIZE; count++)
	{
		if (answers[count] != student[count])
		{
			cout << endl;
			cout << "Answer " << (count + 1) << " is wrong.\n";
		}
		else
		{
			sum += 1;
		}
	}
	
	return sum;
}

int main()
{
	TestGrader testGrader;
	const int SIZE = 20;
	int wrong;
	char answers[SIZE] = {'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B', 
	'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A'};
	char student[SIZE];
	cout << "Driver's License Program Exam Grades";
	cout << "USING ONLY CAPITAL A, B, C, or D AS ANSWERS" << endl;
	testGrader.setKey(answers);
	int sum = testGrader.grade(answers, student);

	wrong = SIZE - sum;
	
	cout << "Student got " << sum << " right. " << endl;
	cout << "Student got " << wrong << " wrong. " << endl;

	if (sum >= 15)
	{
		cout << "Student passed. " << endl;
	}

	else
	{
		cout << "Student failed. " << endl;
	}
	return 0;

}
1
2
3
	char answers[SIZE] = {'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B', 
	'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A'};
	char student[SIZE];


is "student" used to collect the student's answers? You are sending "answers" into the function that collects the student's answers, but you never send in "student". You're just overwriting "answers" every time. "student" doesn't have any initial value, so it's extremely unlucky that whatever junk value was in that memory location matches the answer key.


When I made the suggested change, I get this:

Driver's License Program Exam GradesUSING ONLY CAPITAL A, B, C, or D AS ANSWERS
Enter answer for question 1:
BDAACABACDBCDADCCBDA
Enter answer for question 2:
Enter answer for question 3:
Enter answer for question 4:
Enter answer for question 5:
Enter answer for question 6:
Enter answer for question 7:
Enter answer for question 8:
Enter answer for question 9:
Enter answer for question 10:
Enter answer for question 11:
Enter answer for question 12:
Enter answer for question 13:
Enter answer for question 14:
Enter answer for question 15:
Enter answer for question 16:
Enter answer for question 17:
Enter answer for question 18:
Enter answer for question 19:
Enter answer for question 20:
Student got 20 right.
Student got 0 wrong.
Student passed.
Press any key to continue . . .
how am i supposed to go about that im still confused
You're sending the wrong array into 'setKey'
oh okay i see. when i run it, it compiles but doesnt grade it correctly.
is there something wrong with my calculation
Not from what I can see. I made that one change and it gives the correct output for me. What change did you make?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void TestGrader::setKey(char student[])
{
	for(count = 0; count < 20; count++)
	{
		cout << "Enter answer for question " << (count + 1) << ": " << endl;
		cin >> student[count];
		while(student[count]!= 'A'
			&&student[count] != 'B'
			&&student[count] != 'C'
			&&student[count] != 'D')
			{
				cout << "MUST use CAPITAL A,B,C OR D to answer question(s)" << endl;
			    cin >> student[count];  
			}
	}
}


 
char student[SIZE] = { };

 
testGrader.setKey(student);
Last edited on
oh wait nevermind it compiled correctly
I do not know why it does not work.

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
class TestGrader
{
private:
	static const int SIZE = 20;
	int sum;
	int wrong;
	int count;

public:
	TestGrader();
	void setKey(char[]);
	int grade(char[], char[]);
};

TestGrader::TestGrader()
{}

void TestGrader::setKey(char student[])
{
	for (count = 0; count < 20; count++)
	{
		cout << "Enter student for question " << (count + 1) << ": " << endl;
		cin >> student[count];
		while (student[count] != 'A'
			&&student[count] != 'B'
			&&student[count] != 'C'
			&&student[count] != 'D')
		{
			cout << "MUST use CAPITAL A,B,C OR D to student question(s)" << endl;
			cin >> student[count];
		}
	}
}
int TestGrader::grade(char answers[], char student[])
{
	int sum = 0;

	for (count = 0; count < SIZE; count++)
	{
		if (answers[count] != student[count])
		{
			cout << endl;
			cout << "Answer " << (count + 1) << " is wrong.\n";
		}
		else
		{
			sum += 1;
		}
	}

	return sum;
}

int main()
{
	TestGrader testGrader;
	const int SIZE = 20;
	int wrong;
	char answers[SIZE] = { 'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B',
		'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A' };
	char student[SIZE] = {};
	cout << "Driver's License Program Exam Grades";
	cout << "USING ONLY CAPITAL A, B, C, or D AS ANSWERS" << endl;
	testGrader.setKey(student);
	int sum = testGrader.grade(answers, student);

	wrong = SIZE - sum;

	cout << "Student got " << sum << " right. " << endl;
	cout << "Student got " << wrong << " wrong. " << endl;

	if (sum >= 15)
	{
		cout << "Student passed. " << endl;
	}

	else
	{
		cout << "Student failed. " << endl;
	}
	return 0;

}


produces:


Driver's License Program Exam GradesUSING ONLY CAPITAL A, B, C, or D AS ANSWERS
Enter student for question 1:
B
Enter student for question 2:
D
Enter student for question 3:
A
Enter student for question 4:
A
Enter student for question 5:
C
Enter student for question 6:
A
Enter student for question 7:
B
Enter student for question 8:
A
Enter student for question 9:
C
Enter student for question 10:
D
Enter student for question 11:
B
Enter student for question 12:
C
Enter student for question 13:
D
Enter student for question 14:
A
Enter student for question 15:
D
Enter student for question 16:
C
Enter student for question 17:
C
Enter student for question 18:
B
Enter student for question 19:
D
Enter student for question 20:
A
Student got 20 right.
Student got 0 wrong.
Student passed.
Press any key to continue . . .
thanks i already got it running but if i were to ask the user if he/she wants to grade another student would this be attainable
1
2
3
4
5
6
if(grading == 'Yes' || grading == 'Y')
	{
	    cout << "Enter any letter to continue" << endl;
	    cin >> grading;
	}
	else if(grading == 'No' || grading == 'N')
You could just put the main logic inside a while loop that checks to see if grading is 'y' or whatever you use to signal 'yes'.
But you'll want to put the loop after you declare your variables. You'll also want to reset any variables that might need to be reset at the beginning of the loop.

1
2
3
4
do
{
    //Main logic here
}while(grading == 'y' || grading == 'Y');
Last edited on
Topic archived. No new replies allowed.