Golf score problem

Can someone please help me fix this program? The program is suppose to be of a class that records golf scores on 9 pars, last name, first name, middle initial, calculates total score, and calculates over. It should also be able to print it out.

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
  #include<iostream>
#include<cstdlib>
#include<cmath>
#include<cstddef>
#include<string>
using namespace std;

class golf_info
{
public:
	string last_name;
	string first_name;
	char middle_int;
	int score[9];
	int total;
	int over;
	
	void get_name(string& first_name, string& last_name, char middle_int);
	void get_score(int score[]);
	void calc_score(int total, int over);
	void show_score();
};

int main ()
{
	golf_info player;
	string first_name;
	string last_name;
	char middle_int;
	int number_used;
	int total;
	int over;
	int score[9];
	
	player.get_name(first_name, last_name, middle_int);
	player.get_score(score);
	player.calc_score(total, over);
	player.show_score();
	
	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(2);
	
	return 0;
}
void golf_info::get_name(string& first_name, string& last_name, char middle_int)
{
	cout << "What is player's first name?" << endl;
	cin >> first_name;
	cout << "What is player's middle initial?" << endl;
	cin >> middle_int;
	cout << "What is player's last name?" << endl;
	cin >> last_name;
}

void golf_info::get_score(int score[])
{
	
 score[] = {3, 4, 4, 5, 6, 4, 7, 4, 6};

}

void golf_info::calc_score(int total, int over)
{
	int i;
	
	for (i=0; i<5; i++)
	{
		total = 0;

		total+=score[i];
	}
	 
	over = total - 36;

}

void golf_info::show_score()
{
	cout << "Last name: " << last_name << endl;
	cout << "Middle initial: " << middle_int << endl;
	cout << "First name: " << first_name << endl;
	cout << "Scores: " << score << endl;
	cout << "Total: " << total << endl;
	cout << "Over: " << over << endl;
}


This is the error message I keep getting:
(59) syntax error: ']'
(59) syntax error: missing ';' before '{'
(59) syntax error: missing ';' before '}'
Last edited on
An array's elements can only be assigned all at once like that during initialization where you first declare the array (line 33), using the syntax.

In order to change the elements after initialization, you have to use some sort of loop or just manually punch in the indices.
1
2
3
4
5
6
7
8
9
void golf_info::get_score(int score[])
{
    int temp_array[] = {3, 4, 4, 5, 6, 4, 7, 4, 6};
    for (int i = 0; i < 9; i++)
    {
        score[i] = temp_array[i];
    }

}


btw, in void golf_info::calc_score(int total, int over), you are resetting your "total" variable every loop. You probably want something more along the lines of this:
1
2
3
4
5
6
7
8
{
        total = 0;
	for (int i=0; i<5; i++)
	{
		total+=score[i];
	}
        // ...
}


Edit: Fixed syntax
Last edited on
Just a note, the output errors are pointing to line 63, but the error is actually at 59.


score[] = {3, 4, 4, 5, 6, 4, 7, 4, 6};

Unfortunately, this syntax is not valid. You only do {3, 2, 1} type of defining when you initialize an array.

See below, the following code won't compile.

1
2
3
4
5
6
7
8
int main() {
	int stuff[20];


	stuff[] = { 4, 3, 2 };

	return 0;
}


On the other hand, this will.

1
2
3
4
int main() {
	int stuff[20] = { 4, 3, 2 };
	return 0;
}



And the reason, you can't do

score[] = {3, 4, 4, 5, 6, 4, 7, 4, 6};


is because that the compiler is protecting you from "overfilling" an array. When you say "score[]", the program does not remember whether score has a size of 10 or a size of 100 or a size of 3. If it did have a size of 3 though, allowing your code to run would probably be a very bad thing. You are stepping outside the bounds of your array.
Last edited on
Thank you for fixing the syntax. Once I made the changes Ganado suggested it complied, but when I ran it, it did not work. It didn't output anything for any variables besides middle_int which outputted a * instead of the character that was input.

Am I not passing variables correctly?
Post code again.
Topic archived. No new replies allowed.