Help Clearing Array!

Hi, so I'm pretty new to C++ and I have an assignment where I have to loop the whole program and whenever I loop it, it seems to use the array's numbers again. So I figured that i need to clear it.. I got my friend to try to help and teach me, but I am still learning. If anyone could help me, I would highly appreciate it! Also, if there's anything else wrong with my code, by all means tell me!

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";

static 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() {
cout << "Type 0 to start program, or any integer greater than 1 to close!\n";
double userInput;
cin >> userInput;
while (userInput < 1) {

static float *scores = getScore();
float sum = 0;
float n;
float min = scores[0];
float max = scores[0];
float sum2;
float sum3;

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 throw out the highest and lowest scores, and add up the remaining scores!\n";
for (int b = 0; b < 7; b++) {

if (scores[b] > max)
max = scores[b];

else if (scores[b] < min)
min = scores[b];
}

cout << "Largest number is = " << max << endl;
cout << "Lowest number is = " << min << endl;

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

(sum = sum + scores[a]);

}

sum = sum - max - min;

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

sum2 = sum*n;

cout << "Then we multiply the sum by the difficulty, which is: " << sum2 << endl;

sum3 = sum2*0.6;

cout << "Finally, we multiply the new sum by 0.6 to get our final score: " << sum3 << endl;

cout << "Type 0 to restart program, or any integer greater than 1 to close!\n";

cin >> userInput;



}

return 0;

}
Please add code tags to your post. See http://www.cplusplus.com/articles/jEywvCM9/

Where did you get the idea to use static function variable?
My friend told me to use it. Whenever I just used float, I would get a really weird answer when added together
I've cleaned up your code

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include <string>

using namespace std;

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

    static float scores[7];
    
    for (int i = 0; i < 7; i++) {
    
        scores[i];
        cin >> scores[i];
    
        if ((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() {
    std::string input;
    cout << "Type 0 to start program, or any integer greater than 1 to close!\n";
    double userInput;
    cin >> userInput;
    
    
    static float *scores = getScore();
    float sum = 0;
    float n;
    float min = scores[0];
    float max = scores[0];
    float sum2;
    float sum3;
    
    if (userInput < 1) {
        
            cout << "Now, please enter the degree of dive difficulty! (1.2 - 3.8)\n";
        
            cin >> n;
    
            if ((n < 1.2) || (n > 3.8)) {
                cout << "Please enter a valid number!\n";
                cin >> n;
            }
        
            cout << "First we throw out the highest and lowest scores, and add up the remaining scores!\n";
            
            for (int b = 0; b < 7; b++) {  
                if (scores[b] > max)
                    max = scores[b];
            
                else if (scores[b] < min)
                    min = scores[b];
            }
    
            cout << "Largest number is = " << max << endl;
            cout << "Lowest number is = " << min << endl;
    
            for (int a = 0; a < 7; a++) {
                (sum = sum + scores[a]);
            }
    
            sum = sum - max - min;
    
            cout << "The sum of the remaining scores are: " << sum << endl;
    
            sum2 = sum*n;
    
            cout << "Then we multiply the sum by the difficulty, which is: " << sum2 << endl;
    
            sum3 = sum2*0.6;
    
            cout << "Finally, we multiply the new sum by 0.6 to get our final score: " << sum3 << endl;
    
            cout << "Type 0 to restart program, or any integer greater than 1 to close!\n";
        
            cin >> userInput;
        }
        
    if (input == "quit") {
        return 0;
    }
        
    return 0;

}

Please use [code] tags. Like @keskiverto said.

You could try strcpy (if I'm reading your problem right, you want to clear data from a char array)?

http://www.cplusplus.com/reference/cstring/strcpy/

1
2
3
while (userInput < 1) {

static float *scores = getScore();


I think this may be your problem.

Declaring a local variable as "static" means it will be initialised only once, at the point of definition, and then retain it's value unless it is explicitly changed.

So if you had a function:

1
2
3
4
5
int getSin(int angle)
{
    static int ret = sin(angle);
    return ret;
};


getSin() would initialise ret the first time you call it, thereafter, the value of get remains the same, and the line static int ret = sin(angle); never gets executed again.

From that, you can see that your line static float *scores = getScore(); will only be called once, so you never in fact try to get new values from the user, on subsequent iterations of your while (userInput < 1) loop.

If for some reason you want to stick with the "static" approach (I see you're trying to avoid returning a pointer to a variable which would have been cleaned off the stack, but you don't need it to be static in main(), besides there are much better idioms for accomplishing this), then you'll have to split your declaration/definition into 2 distinct steps:

1
2
static float *scores = 0;
scores = getScore();


Given the structure of your code, consider creating the array of scores in main() and passing it's address to getScores().

Better yet, learn about the standard library containers, and use a suitable one (suggest vector<> or array<>).

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
#include <iostream>
#include <array>

using namespace std;

void gett(array<int, 7> &scores)
{
	for (int i = 0; i < 7; ++i)
	{
                // get user input here
		scores[i] += i;
	}
}

int main()
{
        // fixed-size array with magic number
	array<int, 7> scores = {0};
	for (int i = 0; i < 10; ++i)
	{
		gett(scores);
		for (int j = 0; j < 7; ++j)
		{
			cerr << scores[j] << "\t";
		}

		cout << endl;
	}

	return 0;
}
Topic archived. No new replies allowed.