Need help with arrays

Hello, I'm trying to write this program using arrays that will take in names and grades and compute statistics on them. I am stuck on trying to get the code to print out the name of those who have a score that is above average and below average. If I can figure out the above average, I can do the below average on my own.

When compiling I get an error on line 50 (if (average < grade[i])) saying that the subscript requires array or pointer type. From what I have read online this error is usually caused by using a variable that is not an array as an array. Any help would be appreciated. My book doesn't have anything that can help with 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
51
52
 # include <iostream>
# include <string>
using namespace std;

int main()
{
	int i;
	string names, n = "", high, low;
	double grade, a = 0, total = 0.0, average = 0.0, highest = 0.0, lowest = 101.0;
	const int SIZE = 8;
	int numGrade = 0;
	
	while (n != "DONE" && a != (-1))
	{
		for (i = 0; i < 8; i++)
		{
			cout << "Enter name and grade (or DONE -1 to quit) : ";
			cin >> n >> a;
			
			string names[SIZE] = {n};
			double grade[SIZE] = {a};
			
			if (a != (-1) && n != "DONE")
			{
				numGrade += 1;
				total += a;
				if (a > highest)
				{
					highest = a;
					high = n;
				}
				if (a < lowest)
				{
					lowest = a;
					low = n;
				}
			}
		}
	}
	average = total / numGrade;
	cout << "There were " << numGrade << " grades" << endl;
	cout << "The total was " << total << endl;
	cout << "The average was " << average << endl;
	cout << "The highest grade was " << highest << " and was made by " << high << endl;
	cout << "The lowest grade was " << lowest << " and was made by " << low << endl;
	cout << "The following students made above average: ";
	
	for (i = 0; i < 8; i++)
	{
		if (average < grade[i])
			cout << names[i];
	}
You have a double grade variable declared in the main function.

These are declared locally inside a block, so they are only known within that block.
1
2
string names[SIZE];
double grade[SIZE];

So when you get to the last for loop - it only knows the non-array grade variable that was defined at the top of the main function.

Edit:

If they enter DONE and -1 after entering 4 names and grades - you're still running through the for loop 8 times asking for input. Not sure if that's the result you intended? It seems like you want them to able to quit entering before they've hit the maximum of 8 entries.
Last edited on
Ah, I see it now. Thanks a lot. Also the 8 times was because there were 7 name and grade entries and one entry ending the array. But ill take a look at it. Thank you so much!!!!
Topic archived. No new replies allowed.