Help Getting the Sum in Arrays

Hi, I need some help trying to get the sum from an array. Here is my code so far:

#include <iostream>
#include <string>

using namespace std;

float * getScore() {
cout << "Welcome, enter seven numbers from 0-10 for the judges' score!\n";

float scores[7];

for (int i = 0; i < 7; i++) {

cin >> scores[i];

while ((scores[i] < 0) || (scores[i] > 10)) {

cout << "Please enter a number from 0-10 for the judges' score!\n";

cin >> scores[i];
}
}

return scores;
}

int main() {

float *scores = getScore();

float sum = 0;
float n;

cout << "Now, please enter the degree of dive difficulty! (1.2 - 3.8)\n";

cin >> n;

while ((n < 1.2) || (n > 3.8)) {
cout << "Please enter a valid number!\n";
cin >> n;
}

cout << "First we add up all the scores, then multiply it by the degree of difficulty!\n";

for (int a = 0; a < 7; a++) {

sum = sum + *scores+a;

}

cout << "The sum of the scores are: " << sum << endl;

system("pause");

return 0;
}
You cannot return a pointer to a local variable because the variable becomes invalid as soon as the function ends.

Instead provide scores as a parameter:
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
void getScore(float scores[], int size) { // Note
cout << "Welcome, enter seven numbers from 0-10 for the judges' score!\n";

for (int i = 0; i < size; i++) { // Note

cin >> scores[i];

while ((scores[i] < 0) || (scores[i] > 10)) {

cout << "Please enter a number from 0-10 for the judges' score!\n";

cin >> scores[i];
}
}

return scores;
}

int main() {

float scores[7];

getScore(scores, 7);

...
I think u should declare the array as static to return it i mean getScroe float scores[7] should be like static float scores[7];
Topic archived. No new replies allowed.