A program using array. It doesn't work well!

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
[output]#include <iostream>
using namespace std;

void getScores(double testScores[]);
void showMenu();
char getChoice();
void displayResult(char userAns, double testScores[]);

int main()
{
	const int SIZE = 5;
	double testScores[SIZE];
	
 	 getScores( testScores);
	 showMenu();
	 getChoice();
	 displayResult(getChoice(),  testScores);

	return 0;
}
void getScores(double testScores[])
{
	//double test1, test2, test3, test4, test5;

	cout << "Enter 5 test scores" << endl;

	for (int i = 0;i < 5;i++)
	{
		cin >> testScores[i];
	}
}
void showMenu()
{
	system("cls");
	cout << "A.)   Calculate the average of the test scores" << endl;
	cout << "B.)   Display all test scores" << endl;

}
char getChoice()
{
	char userAns;
	cout << "Enter your choice" << endl;
	cin >> userAns;
	tolower(userAns);
	return userAns;
}
void displayResult(char userAns,double testScores[])
{
	system("cls");

	if (userAns == 'a')
	{
*********double avg = testScores/ 5; // THERE IS SOMETHING IN HERE IT DOES NOT WORK!
	}
	if (userAns == 'b')
		cout << testScores << endl;
}
[/output]
Last edited on
Please use the code tags to format your code like this

std::cout << "Hello World";

and indent the code
First, please use code tags when posting code. See http://www.cplusplus.com/articles/jEywvCM9/

What do you mean by "does not work"?
* Fails to compile?
* Binary crashes?
* Unexpected results?

We can already see multiple issues in your code but it is very important that you learn to explain and analyse the situation.
look at the last function, i have ******* next to the problem. The array need some more information. I don't know what to add.
I want to divide the values inside the array by 5, i just can't access the info
Last edited on
The function parameter testScores is a pointer to the first element of the array, not an array.

If you want to use testScores, you need to use the subscript operator [] to access the elements of the array.

testScores[0] is the first element of the array
testScores[1] is the second one, and so on.

You can't divide a pointer and an integer.
testScores is an array, it's not an actual value. You need to get the value from the array and compute.
If the compilation fails due to code that the compiler does not understand, then the compiler gives an error message. One compiler says this about your code:
 In function 'char getChoice()':
44:17: warning: statement has no effect [-Wunused-value]
 In function 'void displayResult(char, double*)':
53:26: error: invalid operands of types 'double*' and 'int' to binary 'operator/'
53:8: warning: unused variable 'avg' [-Wunused-variable]

It does point to line 53 (just like you did) and says that you have there double* and int operands.
You have testScores / 5, where 5 is indeed int and testScores is double [] according to line 47.
No disagreement there.

Learn to read the compiler messages. Learn to show them too.


Look at lines 27-30 of your code. There you do access each element of the array, don't you?
Topic archived. No new replies allowed.