Help with comparing arrays

I have an assignment to write a program to grade a multiple choice quiz. I do not have to ask each question, only compare input to the correct answers. I have a few problems to work out, I have noted the problem areas below on lines 26, 29, and 57. Any help would be greatly appreciated!

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
// Tanya Reed Lab8.cpp

#include <iostream>
#include <cstring>
#include <iomanip>
#include <cmath>
using namespace std;

// This program grades grades an online quiz
// The program ask students to enter multiple-choice 
// answers A, B, C, or D, for 10 questions.
// Then compares their answers to the correct
// answers and determines their grade.

void inputAnswers(char given[]);

int main()
{
	const int size = 10;
	char correctAnswers[size] = { 'B', 'C', 'A', 'D', 'B', 'A', 'D', 'C', 'A', 'B' };
	int numRight = 0;
	char given[size];

	inputAnswers(given);

	numRight(given, correctAnswers);  //numRight Error states expression preceding () of apparent call must have (pointer-to-) fuction type

	{
		cout << "Your quiz grade is" << numRight(correctAnswers, given) * 10 << "% \n";  //Same error for numRight as above (line 26)
	}
	return 0;
}

//*******************************
//This function gets the answers*
//*******************************

void inputAnswers(char given[])
{
	for (int i = 0; i <= 9; i++)
	{
		cout << "Please enter your answer for question # " << i + 1 << ": ";
		cin >> given[i];
	}
	return;
}

//********************************************************
//This function finds the number of correct answers given*
//********************************************************
int numRight(int numRight[])
{
	int correct = 0;

	for (int count = 0; count >= -1; count++)
	{
		if (correctAnswers == inputAnswers) // Error says correctAnswers is undefined, isn't that what I did in the main()
			correct += numRight[count];
	}
	return correct;
}
You need a function prototype for numRight like you do for inputAnswers on line 15.

You send two arrays to the numRight function in two different orders, but you only define it to take one array.

line 26 numRight(given, correctAnswers);
line 29 numRight(correctAnswers, given)
line 51 int numRight(int numRight[])

line 57 - the numRight function knows the local variable correct and the array that's been sent to it as numRight. It doesn't know anything called correctAnswers or inputAnswers. inputAnswers is a function that returns nothing, so it doesn't make sense to call it anyway as you have on here.

You have to declare functions before you can call them. At linse 26 and 29, you're calling numRight() before it has been declared, but the error message is confusing because you do have a variable named numRight, so the compiler gets confused.

At line 57, it's complaining because correctAnswers isn't defined. The one that you defined in main() is a local variable, only visible to main().

Let's work backwards to fix this. First, the parameters to numRight() should be the ones at line 26 (the answers given, and the correct answers). You should also pass in the number of questions so you know when to stop

1
2
3
4
5
6
7
8
9
10
11
int numRight(int given[], int correctAnswers[], int numQuestions)
{
	int correct = 0;

	for (int count = 0; count < numQuestions; count++)
	{
		if (correctAnswers[count] == given[count])
			correct++;  // increment correct by one.
	}
	return correct;
}


Returning to the top you need to declare function numRight around line 15:
int numRight(int given[], int correctAnswers[], int numQuestions);

Now change the call at line 29 to use the 3 arguments. Be sure to pass "given" and "correctAnswers" in the right order.

Finally, I think you can delete the call at line 26. It doesn't do anything.

wildblue, inputAnswers was part of the assignment. It request the user to enter answers for question 1, question 2, etc.

dhayden, I am working on it now, I must admit it seems to be bringing up more errors. Every time I correct one error 3 more appear.
I am down to two errors now!

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

#include <iostream>
#include <cstring>
#include <iomanip>
#include <cmath>
using namespace std;

// This program grades grades an online quiz
// The program ask students to enter multiple-choice 
// answers A, B, C, or D, for 10 questions.
// Then compares their answers to the correct
// answers and determines their grade.

int numRight(int given[], int correctAnswers[], int numQuestions);

void inputAnswers(char given[]);

int main()
{
	const int size = 10;
	char correctAnswers[size] = { 'B', 'C', 'A', 'D', 'B', 'A', 'D', 'C', 'A', 'B' };
	int numQuestions[10];
	int numRight = 0;
	char given[size];

	inputAnswers(given);

	numRight(given, correctAnswers, numQuestions); //numRight error - expression preceding () of apparent call must have(pointer-to-) function type

	{
		cout << "Your quiz grade is" << numRight(given, correctAnswers, numQuestions) * 10 << "% \n"; //numRight error, same as line 29
	}
	return 0;
}

//*******************************
//This function gets the answers*
//*******************************

void inputAnswers(char given[])
{
	for (int i = 0; i <= 9; i++)
	{
		cout << "Please enter your answer for question # " << i + 1 << ": ";
		cin >> given[i];
	}
	return;
}

//********************************************************
//This function finds the number of correct answers given*
//********************************************************
int numRight(int given[], int correctAnswsers[], int numQuestions)
{
	char correctAnswers[10] = { 'B', 'C', 'A', 'D', 'B', 'A', 'D', 'C', 'A', 'B' };
	int correct = 0;

	for (int count = 0; count < numQuestions; count++)
	{
		if (correctAnswers[count] == given[count])
			correct++;
	}
	return correct;
}
I found the error and corrected it. Now it is giving a new error, on the same two lines, error C2064: term does not evaluate to a function taking 3 arguments. When I remove one the error remains but states 2 instead of 3.
Line 23: You're declaring numRight as a local int. THis takes precedence over the global function prototype on line 14.

Line 28: You can't call an int (numRight).
I changed that to a const int and that is when the new error C2064 occurred, which I am still working on
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

#include <iostream>
#include <cstring>
#include <iomanip>
#include <cmath>
using namespace std;

// This program grades grades an online quiz
// The program ask students to enter multiple-choice 
// answers A, B, C, or D, for 10 questions.
// Then compares their answers to the correct
// answers and determines their grade.

int numRight(int given[], int correctAnswers[], int numQuestions);

void inputAnswers(char given[]);

int main()
{
	const int size = 10;
	char correctAnswers[size] = { 'B', 'C', 'A', 'D', 'B', 'A', 'D', 'C', 'A', 'B' };
	int numQuestions[10];
	const int numRight = 0;
	char given[size];

	inputAnswers(given);

	numRight(given, correctAnswers, numQuestions); //error C2064: term does not evaluate to a function taking 3 arguments

	{
		cout << "Your quiz grade is" << numRight(given, correctAnswers, numQuestions) * 10 << "% \n"; //error C2064, same as Line 29
	}
	return 0;
}

//*******************************
//This function gets the answers*
//*******************************

void inputAnswers(char given[])
{
	for (int i = 0; i <= 9; i++)
	{
		cout << "Please enter your answer for question # " << i + 1 << ": ";
		cin >> given[i];
	}
	return;
}

//********************************************************
//This function finds the number of correct answers given*
//********************************************************
int numRight(int given[], int correctAnswsers[], int numQuestions)
{
	char correctAnswers[10] = { 'B', 'C', 'A', 'D', 'B', 'A', 'D', 'C', 'A', 'B' };
	int correct = 0;

	for (int count = 0; count < numQuestions; count++)
	{
		if (correctAnswers[count] == given[count])
			correct++;
	}
	return correct;
}
Last edited on
numRight is supposed to be a function. You don't need to declare it as an int variable on line 23, const or not.

You don't need to call the function twice - line 28 is unnecessary.

You do need to make sure the parameters agree with what you are sending. The given and correctAnswers arrays are char, not int.

line 14 int numRight(int given[], int correctAnswers[], int numQuestions);
line 53 int numRight(int given[], int correctAnswsers[], int numQuestions) edit <- check spelling there too :)

If you're passing the correctAnswer array from main, you don't need to declare a new correctAnswers array in the function on line 55.


Edit - Is numQuestions supposed to be another array or is it just the value 10? You're using it as an integer but have declared it as an array on line 22.
Last edited on
I got it! Thanks everyone for the help!
Tanya
Topic archived. No new replies allowed.