Help With Coding

Hi, I am in a beginner class learning C++ and we have a project where we are taking dives from an Olympic ruling and making a code to display the total scores as well as names, countries, ages and all that.

I have the code working except for displaying the final scores. I know they are there as I have checked with a quick cout but it's not showing up where I want it.

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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Alexandra Kraft                       12/02/2014
// Lab N - Olympic (or other) Diving

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>
using namespace std;

void read_data(string country[], string Dname[], int age[], int size, double DD, double score[], double final_score[]);
double bubble_sort(double score[], double DD);

int main()
{
	string Dname[6];
	string country[6];
	int age[6];
	double final_score[6];
	int size = 6;
	double DD = 0;
	double score[6];

	cout << fixed << showpoint << setprecision(2);

	read_data(country, Dname, age, size, DD, score, final_score);



	system("pause");
	return 0;
}


void read_data(string country[], string Dname[], int age[], int size, double DD, double score[], double final_score[])
{
	ifstream dataIn;
	dataIn.open("diving.txt");
	if (dataIn.fail())
	{
		cout << "File does not exist." << endl;
		system("pause");
		exit(1);
	}
	int i, count;
	double answer;
	for (i = 0; i < size; i++)  // diver
	{
		getline(dataIn, country[i]);
		getline(dataIn, Dname[i]);
		dataIn >> age[i];
		double total = 0;
		for (count = 0; count < 5; count++)  // dive
		{
			dataIn >> DD;
			dataIn >> score[0];
			dataIn >> score[1];
			dataIn >> score[2];
			dataIn >> score[3];
			dataIn >> score[4];
			dataIn >> score[5];
			dataIn >> score[6];
			answer = bubble_sort(score, DD);
			total = total + answer;
		}
		dataIn.get();
	}




	int counter;
	for (counter = 0; counter < 6; counter++)
	{
		cout << "Rank " << counter + 1 << ": " << final_score[counter] << " " << Dname[counter] << " " << age[counter] << " " << country[counter] << endl;
	}
	dataIn.close();
}

double bubble_sort(double score[], double DD)
{
	vector<double>judge(7);

	judge[0] = score[0];
	judge[1] = score[1];
	judge[2] = score[2];
	judge[3] = score[3];
	judge[4] = score[4];
	judge[5] = score[5];
	judge[6] = score[6];

	double temp;
	int counter;
	bool swap;

	do
	{
		swap = false;
		for (counter = 0; counter < 6; counter++)
		{
			if (judge[counter] < judge[counter + 1])
			{
				temp = judge[counter];
				judge[counter] = judge[counter + 1];
				judge[counter + 1] = temp;
				swap = true;
			}

		}
	} while (swap == true);

	judge.pop_back();
	judge.pop_back();
	reverse(judge.begin(), judge.end());
	judge.pop_back();
	judge.pop_back();

	double thisDive = (judge[0] + judge[1] + judge[2])*DD;
	return thisDive;
}



Here is the text file as 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
China
Minxia WU
26
3.0
9.0 9.0 9.0 8.5 8.5 9.0 8.5
2.9
9.5 9.5 9.0 9.0 8.5 9.5 9.0
3.1
9.0 9.5 9.5 9.0 9.5 9.0 9.0
3.0
9.5 9.5 9.0 9.5 9.5 9.0 9.0
3.0
10.0 9.5 9.5 9.0 9.5 9.0 9.5
China
Zi HE
21
3.0
8.5 9.0 8.5 9.0 8.5 8.5 8.5
3.1
9.5 9.5 9.0 8.5 9.0 9.0 9.0
3.0
9.0 8.5 8.5 9.0 9.0 8.5 7.5
3.0
8.5 8.5 8.5 8.5 8.0 8.5 8.5
3.0
7.0 7.5 7.0 7.0 6.5 7.5 7.5
Mexico
Laura SANCHEZ SOTO
26
3.0
8.0 8.0 7.5 8.0 8.0 7.5 7.5
3.0
7.5 8.0 7.5 7.5 7.5 7.5 7.5
3.0
8.0 8.5 8.0 8.5 7.5 8.5 8.5
3.1
7.5 8.0 7.5 8.0 8.0 8.0 8.0
3.0
8.0 8.5 8.0 8.5 8.0 8.5 9.0
Italy
Cagnottoa TANIA
27
3.0
9.0 8.0 8.5 8.5 9.0 8.5 8.5
3.0
8.0 8.0 7.5 8.0 7.5 7.5 6.5
3.1
7.0 8.0 7.5 7.0 7.5 7.0 7.5
3.0
8.0 8.0 8.0 8.0 8.0 8.0 7.5
3.0
9.0 8.5 9.0 8.5 8.5 8.5 7.5
Australia
Sharleen STARTTON
24
3.0
8.0 8.0 8.0 7.5 8.0 7.5 7.5
3.0
7.5 7.5 7.5 7.5 8.0 7.5 7.5
3.1
7.0 7.5 7.5 7.0 7.0 7.0 7.5
3.0
7.5 8.0 7.5 8.0 8.0 7.5 7.5
3.0
8.5 8.0 8.0 8.0 8.5 8.0 8.0
Canada
Jennifer ABEL
20
3.0
8.0 7.5 7.0 7.5 7.0 7.5 7.0
3.1
8.5 8.5 8.5 8.0 8.0 8.5 8.0
3.0
6.5 7.0 6.0 6.5 5.5 6.0 6.0
3.0
8.0 8.0 8.0 8.0 7.0 8.0 7.5
3.0
8.0 8.0 8.0 8.0 8.0 8.5 8.5
morning,
I know they are there as I have checked with a quick cout

where did you put this cout?
I had put it under total = total + answer;

I've since removed it because it gave the right answers and I just want the code to display

cout << "Rank " << counter + 1 << ": " << final_score[counter] << " " << Dname[counter] << " " << age[counter] << " " << country[counter] << endl;
Most of your arrays do have 6 elements but you do use 7. The last one is unallocated. Accessing it, especially for modifying its contents may result in undefined behaviour.

TIP: Use a globally defined constant f.e.

const unsigned RecordCount(7);

and use it as size indicator of your array declarations and part of loop termination conditions.

Another minor tip: A function should encapsulate exactly the one functionality as indicated by its name if possible. So read data shouldn't output anything but debugging infos. Write a separate function to print data. The same applies to sorting and calculating an average value (or change a functions name to indicate its real behavior).

And finally you're not using variable total at line 65.
I originally had a void report_data but a friend of mine who is in the same class said that she did it all from her read_data and it was my professor who put the answer = bubble_sort in there as well.

I'm not sure what you mean by:
And finally you're not using variable total at line 65.
You're assigning variable total a value but never use it. (Only for self-assigning). Its not an error but its useless. An optimizer may remove the resulting code from your executable.

As I've said: Function names should reflect a functions activity as close as possible and vice versa.
I've changed total into final_score but the same thing still happens. I just get random long numbers in place where the score should be.

Is there a way to convert either total into final_score or something else I can use?
can you post your updated read_data() method please?
I just get random long numbers in place where the score should be.


Aaahh, that's your problem! You've never told us (if I'd read all your comments correctly).
Aaahh, that's your problem!

i'm not sure why you're surprised? OP is printing out the contents of their final_score array on line 76, but (as far as i can see), never puts anything in there in the first place!
Last edited on
Sorry, I'm reading too fast and didn't got that.

I have the code working except for displaying the final scores.
OP, in terms of your final_result array, this is what you're doing:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

int main() {

	double final_score[6];

	for (int i = 0; i < 6; i++)
	{
		std::cout << final_score[i] << std::endl;
	}

	return 0;
}

You're just creating an array with 6 uninitialised doubles in it and then printing it out to the screen.
As tcs said earlier it looks like you want to assign each 'total' variable to each element in final_score.
Topic archived. No new replies allowed.