Function returning array, subscript error

I've only been coding for about a month, so bare with me. In my programming class the assignment is to write a program, using arrays and functions, to:
-have the user input their test answers
-compare that to the given test answers
-calculate the score

The only error that is coming up is: "expression must have pointer-to-object type"/"subscript requires array or pointer type".

The arrays I did for the previous project looked the exact same and they were fine. The instructions call for functions to return the array values. Any help would be 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
#include<iostream>
using namespace std;

//Function Prototypes
void inputAnswers(int givenAnswers[]);
void numberCorrect(int);
int numRight;
const int SIZE = 10;
int correctAnswers[SIZE] = { 5, 22, 35, 0, 16, 28, 4, 3,42, 51 };
int givenAnswers[SIZE];

int main()
{
	

	inputAnswers(givenAnswers);
	numberCorrect(numRight);		
		
	cout << "Your grade is: " << (numRight * 10) << "%" << endl;
			
		
		return 0;

}


int inputAnswers(int givenAnswers)
{

	for (int i = 0; i < SIZE; i++)
	{
		cout << "Enter answer for question #" << i + 1 << endl;
		cin >> givenAnswers[i];
	}
	return givenAnswers;
}

int numberCorrect(int givenAnswers, int correctAnswers)
{
	for (int i = 0; i < SIZE; i++)
	{
		if (givenAnswers[i] == correctAnswers[i])
			numRight++;
	}
	return numRight;
}
Well, fist your actual functions doesn't match the prototypes... inputAnswers() should return void and take an array of ints, and numberCorrect() should take only one int, according to your function prototypes.

Second, inputAnswer()'s prototype is correct, but for numberCorrect() it's supposed to be int numberCorrect(int givenAnswers[], int correctAnswers[]);. Notice that we have to pass in arrays in order for your code to work.

Last, if you want numberCorrect() to take two int[]s and return the number of right answers (which I suggest you to do), you have to add int numRight = 0; before your for loop in numberCorrect(). And when you call it, you call it like this:
numRight = numberCorrect(givenAnswers, correctAnswers);.

Hope this helps. :)
Finally got it to 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
#include<iostream>
using namespace std;

//Function Prototypes
void inputAnswers(int []);
int numberCorrect(int [], int []);
const int SIZE = 10;
int numRight = 0;
int correctAnswers[SIZE] = { 5, 22, 35, 0, 16, 28, 4, 3, 42, 51 };
int givenAnswers[SIZE];


int main()
{
	

	inputAnswers(givenAnswers);
	numRight = numberCorrect(givenAnswers, correctAnswers);
		
	cout << "Your grade is: " << (numRight * 10) << "%" << endl;
			
		
		return 0;

}


void inputAnswers(int givenAnswers[])
{

	for (int i = 0; i < SIZE; i++)
	{
		cout << "Enter answer for question #" << i + 1 << endl;
		cin >> givenAnswers[i];
	}
}


int numberCorrect(int givenAnswers[], int correctAnswers[])
{
	
	for (int i = 0; i < SIZE; i++)
	{
		if (givenAnswers[i] == correctAnswers[i])
			numRight++;
	}
	return numRight;
}
Last edited on
Topic archived. No new replies allowed.