Please help by pointing me the right path for this assignment

I have the following assignment:

Make a program that is used as an information system for the World competition in Athletics. The program should keep and modify information about athletes, as for each athlete -> ( number in the competition, name, discipline, the best result in his career, the best result for this year, number of participations in world competitions). There must be 5 different disciplines (200m Sprint, 4x400m, Throwing spear, high jump, triple jump). In each discipline, there will be 6 athletes.


B. Adding athletes i. Add one athlete in each discipline. ii. Add a list of athletes, write number N and after it N athletes per discipline.

C. Show all athletes on the screen

D. Run a competition

i. Runs randomly for each athlete. The athlete can score no less than 20% of his best score for the year and no more than 10% of his best score for the career.

ii. There is a 5% chance for each athlete to do a fault-start.

E. Table of medals - athletes in each competition are grouped by countries in a new array(1st place-30p, scnd - 20p, etc, etc). The array is shown on screen in increasing order (also showing the name of country, points, list of athletes from this country, their scores during the competition)

------
So far I managed to make a class, constructor, destructor, few functions which allow me to add members based on user input through a vector and also show these athletes on the screen by request. So now I have to do "D." point where running the competition should generate scores for each athlete with the parameters described below. I believe I have to make a new class for that but it is hard for me to form the idea of how it will generate and most importantly assign these scores to the athletes as later on in the program I will have to make a function/class that prints athletes ordered based on these generated results. I was thinking to generate the results with rand() but I am not really sure about it. Any ideas are most welcome!!!

-----
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
class Athlete 
{
protected:
	int competition_number;
	std::string name,discipline;
	float BestOfYear, BestOfCareer;
	double NumOfCompetitions;
	static int numOfAthletes;

public:
	std::string GetName() { return name; }
	void SetName(std::string name) { this->name = name; }
	
	int GetCompetitionNumber() { return competition_number; }
	void SetCompetitionNumber() {
		competition_number = rand() % 1000 + 1;
		this->competition_number = competition_number;
	};
	float GetBestOfYear() { return BestOfYear; }

	float GetBestOfCareer() { return BestOfCareer; }

	std::string GetDiscipline() { return discipline; }

	void SetDiscipline(std::string discipline) { this->discipline = discipline;}

	void SetAll(std::string, string, float, float, double);

	Athlete(std::string, string, float, float, double);

	Athlete();

	~Athlete();

	static int GetNumOfAthletes() { return numOfAthletes; }

	void ToString();

	void AddSingleAthlete();

	void AddMultipleAthletes();

	float RandomResultSprint(float, float);

	void ShowAll(std::vector<Athlete>athletes);

	std::vector<Athlete> athletes;
};
                  
int Athlete::numOfAthletes = 0;
Athlete::Athlete(std::string name, std::string discipline, float BestOfYear, 
	float BestOfCareer, double NumOfCompetitions) {
	this->name = name;
	this->discipline = discipline;
	this->BestOfYear = BestOfYear;
	this->BestOfCareer = BestOfCareer;
	this->NumOfCompetitions = NumOfCompetitions;
	SetCompetitionNumber();
	Athlete::numOfAthletes++;
}

Athlete::Athlete() {
	this->name = "";
	this->discipline = "";
	this->BestOfYear = 0;
	this->BestOfCareer = 0;
	this->NumOfCompetitions = 0;
	SetCompetitionNumber();
	Athlete::numOfAthletes++;
}

Athlete::~Athlete() 
{
	std::cout << "Athlete " << this->name << " deleted\n";
}

void Athlete::ToString() {
	std::cout <<"Athlete with competition number "<<this->competition_number<< " has the name " << 
		this->name << " .\n"<<this->name<<" is  competeing in " <<
		this->discipline << " and "<<this->name<<" has participated in " <<
		this->NumOfCompetitions << " World Competitions. "<<
		"\n"<<this->name<<"'s best result for this year is "<<this->BestOfYear<<
		" while the best for his career is "<<this->BestOfCareer<<"\n\n";
}

void Athlete::AddSingleAthlete(){
	std::string Name;
	std::string Discipline;
	float bestOfYear, bestOfCareer;
	double NumOfParticipations;
	int Answer = 1;
	do
	{
		cout << "Enter the info of the Athlete bellow:\n";
		cout << "Athlete's name:  ";
		cin >> Name;
		cout << endl << "Athlete's discipline: ";
		cin >> Discipline;
		cout << endl << "Athlete's best result for the current season: ";
		cin >> bestOfYear;
		cout << endl << "Athlete's best result for the whole career: ";
		cin >> bestOfCareer;
		cout << endl << "Athlete's number of World Competitions: ";
		cin >> NumOfParticipations;

		cout << endl << "Do you wish to add another athlete?\n1:  Yes\n0:  No\n";
		cin >> Answer;
		Athlete nqkoi(Name, Discipline, bestOfYear, bestOfCareer, NumOfParticipations);
		athletes.push_back(nqkoi);
	} while (Answer != 0);
}

void Athlete::AddMultipleAthletes() {
	std::string Name;
	std::string Discipline;
	float bestOfYear, bestOfCareer;
	double NumOfParticipations;
	int Answer = 1;
	int numOfAthletes = 0;

	do
	{
		cout << "How many athletes would you like to add to each discipline?\n";
		cin >> numOfAthletes;
		for (int b = 0; b < numOfAthletes; b++)
		{
			cout << "\nEnter the info of the Athletes bellow (one at a time):\n";
			cout << "Athlete's name:  ";
			cin >> Name;
			cout << endl << "Athlete's discipline: ";
			cin >> Discipline;
			cout << endl << "Athlete's best result for the current season: ";
			cin >> bestOfYear;
			cout << endl << "Athlete's best result for the whole career: ";
			cin >> bestOfCareer;
			cout << endl << "Athlete's number of World Competitions: ";
			cin >> NumOfParticipations;

			Athlete nqkoi(Name, Discipline, bestOfYear, bestOfCareer, NumOfParticipations);
			athletes.push_back(nqkoi);
		}
		cout << endl << "Do you wish to add another athlete?\n1:  Yes\n0:  No\n";
		cin >> Answer;
	} while (Answer == 1);
}

float Athlete::RandomResultSprint(float BestOfYear, float BestOfCareer) 
{
	float RandMax, RandMin, Result;
	
	for (int a = 0; a < athletes.size(); a++)
	{
		Athlete& nqkoi = athletes.at(a);
		RandMin = nqkoi.GetBestOfYear() * 0.8;
		RandMax = nqkoi.GetBestOfCareer() * 1.1;
		RandMin = RandMin * 100;
		RandMax = RandMax * 100;
		//cout << RandMax << "    " << RandMin << endl;
		do
		{
			Result = rand() % (int)RandMax + RandMin;

		} while (Result > RandMax || Result < RandMin);
		Result = (float)Result;
		Result = Result / 100;
		cout << "The randomly generated result for " << nqkoi.name << "is " << Result << endl;
	}
	return Result;
}

int main()
{
	srand(time(NULL));
	int choice;
	int choiceAdd;
	const int Add_choice = 1,
		Show_choice = 2,
		Run_choice = 3,
		Medals_choice = 4,
		Check_choice = 5,
		Quit_choice = 6;
	
	Athlete edin;
	do{
		cout << "Please choose an option:" << endl << endl
			<< "1. Add an athlete/athletes" << endl
			<< "2. Show all athletes" << endl
			<< "3. Run a competition" << endl
			<< "4. Table with medals (Only choose after you have ran a competition)" << endl
			<< "5. Athletes check (Ordered by age or name)" << endl
			<< "6. Quit/Exit" << endl;
	cin >> choice;
	while (choice<Add_choice || choice>Quit_choice)
	{
		cout << "Please enter a valid menu choice (1 to 6): ";
		cin >> choice;
	};
	if (choice != Quit_choice)
	{
		switch (choice)
		{
		case Add_choice:		
			cout << "Please choose an option:" << endl << endl
				<< "1. One athlete for each discipline" << endl
				<< "2. List of athletes for each discipline" << endl;
			cin >> choiceAdd;
			while (choice<1 || choice>2)
			{
				cout << "Please enter a valid menu choice (1 to 6): ";
				cin >> choiceAdd;
			};
			switch (choiceAdd)
			{
			case 1:
				edin.AddSingleAthlete();
				break;
			case 2:
				edin.AddMultipleAthletes();
				break;
			}
		case Show_choice:
			edin.ShowAll(edin.athletes);
			break;		
		case Run_choice:
			edin.RandomResultSprint(edin.GetBestOfYear(), edin.GetBestOfCareer());
			break;
		}
	}
}while (choice != Quit_choice);
	
	std::cout << "Number of athletes is " << Athlete::GetNumOfAthletes() << "\n";
	return 0;
}

Last edited on
I removed a bit of my code for the post as it was too long.

I forgot to mention that I am beginner in C++ but probably you can see that in the code ..

Any ideas are most welcome!
Last edited on
The big problem that I see right now is that you're trying to jam everything into the one Athlete class. I wrote code for this (all except part E) and came up with (1) an Athlete, which is a single person, (2) a Discipline, which has a name and a vector of Athletes participating in it, and (3) a vector of Disciplines, which are the 5 disciplines required. Add some methods to enter the data, print the results and run the races, and the code gets pretty easy.

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 <vector>
#include <string>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;

struct Athlete {
    unsigned id;		// ID number in competition
    string name;
    string country;		// Gotta store it somewhere!
    double careerBest;		// best score of career
    double yearBest;		// best score this year
    unsigned numCompetitions;	// number of competitions
    double currentScore;	// score for the current
				// competition. Negative value means
				// false start

    // Note that we don't record the discipline. That's implied by
    // their location in the competition array below
    
    // prompt user for info on this athlete
    void getInfo();
    
    // Run a competition and update currentScore
    // There is a 5% change if a fault start. Otherwise
    // the score is a random number between 80% of the yearly
    // best and 110% of career best.
    void run();

    void print();
};

struct Discipline {
    string name;		// name of the discipline
    vector<Athlete> athletes;	// the athletes participating in it
    void print();
};

vector<Discipline> competition;	// The world competition!

// Add one athlete to each discipline
void addOneEach()
{
...
}

// Add N athletes to each discipline.
void addN()
{
...
}

// I LEFT OUT THE IMPLEMENTATION OF THE METHODS, BUT EACH ONE IS
// PRETTY EASY TO WRITE.


int main()
{
    // This is just a test program see if the rest of it works.
    //

    // initialize the competition
    Discipline dummy;
    dummy.name = "200m Sprint";
    competition.push_back(dummy);
    dummy.name = "4x400m";
    competition.push_back(dummy);
    dummy.name = "Throwing spear";
    competition.push_back(dummy);
    dummy.name = "high jump";
    competition.push_back(dummy);
    dummy.name = "triple jump";
    competition.push_back(dummy);

    addN();

    // Run the competitions
    for (unsigned i=0; i<competition.size(); ++i) {
	for (unsigned racer = 0; racer < competition[i].athletes.size(); ++racer) {
	    competition[i].athletes[racer].run();
	}
    }

    // Print the results
    cout << "\n\n---------- COMPETITION RESULTS -------------\n";
    for (unsigned i=0; i<competition.size(); ++i) {
	competition[i].print();
    }
}

That is amazing! Thank you so much for spending your time to help me. And definitely thank you for leaving the methods for me, I have to learn something out of all this in the end!

Just a few more questions (no code please! :D )

So do you think that I should use rand() for generating the results? If so, how do i set the limits, when the limits are of type double and rand() need an integer for a limit?

Also for the run() method, I am supposed to pass careerBest and yearlyBest as parameters so I can use them inside the method?

Once again, I am incredibly grateful for helping me learn! My programming professor is not doing that, so forums are my best friends now
I made this but then what arguments should I pass to this function in the main() ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void run(double careerBest, double yearlyBest, double currentScore) {
	int faultStart;
	double RandMax = careerBest * 1.1;
	double RandMin = yearlyBest * 0.8;
	
	faultStart = rand() % 100;
	if (faultStart <= 5)
		currentScore = -1;
	else
	{
		do
		{
			currentScore = (RandMax - RandMin) * ((double)rand() / (double)RAND_MAX) + RandMin;

		} while (currentScore > RandMax || currentScore < RandMin);
	}
	
}
Last edited on
Okay, it is running now but untill certain moment. I add 1 athlete per discipline and then it just don't do anything else.
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <iostream>
#include <vector>
#include <string>
#include <ctime>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;

struct Athlete {
	unsigned id;		// ID number in competition
	string name;
	string country;		// Gotta store it somewhere!
	double careerBest;		// best score of career
	double yearBest;		// best score this year
	unsigned numCompetitions;	// number of competitions
	double currentScore;	// score for the current
				// competition. Negative value means
				// false start

	// Note that we don't record the discipline. That's implied by
	// their location in the competition array below

	// prompt user for info on this athlete
	void getInfo() {
		Athlete someone;
		cout << "Enter the info of the Athlete bellow:\n";
		cout << "Athlete's name:  ";
		cin >> someone.name;
		cout << "Athlete's country of origin: ";
		cin >> someone.country;
		cout << endl << "Athlete's best result for the current season: ";
		cin >> someone.yearBest;
		cout << endl << "Athlete's best result for the whole career: ";
		cin >> someone.careerBest;
		cout << endl << "Athlete's number of World Competitions: ";
		cin >> someone.numCompetitions;
	};

	// Run a competition and update currentScore
	// There is a 5% change if a fault start. Otherwise
	// the score is a random number between 80% of the yearly
	// best and 110% of career best.
	void run();
};

void Athlete::run() {
	Athlete someone;

	unsigned faultStart;
	double RandMax = someone.careerBest * 1.1;
	double RandMin = someone.yearBest * 0.8;
	
	faultStart = rand() % 100;
	if (faultStart <= 5)
		someone.currentScore = -1;
	else
	{
		do
		{
			someone.currentScore = (RandMax - RandMin) * ((double)rand() / (double)RAND_MAX) + RandMin;

		} while (someone.currentScore > RandMax || someone.currentScore < RandMin);
	}
	
}


struct Discipline {
	string name;		// name of the discipline
	vector<Athlete> athletes;	// the athletes participating in it
	void print();
};

vector<Discipline> competition;
	// The world competition!
void Discipline::print() {
	for (unsigned i = 0; i < 1; ++i) {
		for (unsigned racer = 0; racer < competition[i].athletes.size(); ++racer) {
			cout << competition[i].athletes[racer].name << " scored " <<
				competition[i].athletes[racer].currentScore << " in 200m Sprint for his country - " <<
				competition[i].athletes[racer].country;
		}
	}
}
// Add one athlete to each discipline
void addOneEach(){
	for (unsigned i = 0; i < competition.size(); i++)
	{
		Athlete someone;
		someone.getInfo();
		competition[i].athletes.push_back(someone);
	}
}

// Add N athletes to each discipline.
void addN()
{
	unsigned NumToAdd;
	cout << "How many athletes do you wish to add?";
	cin >> NumToAdd;
	
	for (unsigned i = 0; i < competition.size(); i++)
	{
		for (unsigned racer = 0; racer < NumToAdd; ++racer) {
			Athlete someone;
			someone.getInfo();
			competition[i].athletes.push_back(someone);

		}
	}
}

// I LEFT OUT THE IMPLEMENTATION OF THE METHODS, BUT EACH ONE IS
// PRETTY EASY TO WRITE.


int main()
{
	unsigned srand(time(NULL));
	// This is just a test program see if the rest of it works.

	// initialize the competition
	Discipline dummy;
	dummy.name = "200m Sprint";
	competition.push_back(dummy);
	dummy.name = "4x400m";
	competition.push_back(dummy);
	dummy.name = "Throwing spear";
	competition.push_back(dummy);
	dummy.name = "high jump";
	competition.push_back(dummy);
	dummy.name = "triple jump";
	competition.push_back(dummy);

	addOneEach();

	// Run the competitions
	for (unsigned i = 0; i < competition.size(); ++i) {
		for (unsigned racer = 0; racer < competition[i].athletes.size(); ++racer) {
			competition[i].athletes[racer].run();
		}
	}

	// Print the results
	cout << "\n\n---------- COMPETITION RESULTS -------------\n";
	for (unsigned i = 0; i < competition.size(); ++i) {
		competition[i].print();
	}
	return 0;
}



Am I on the right track?
yes and no.
So do you think that I should use rand()?
No one stopped you in time. use <random> … see the examples in the reference on this site. rand() is a relic from C and is low quality all around (not because its from C but because its very old and was written for performance first at the cost of utility).
Ughh, f** my life right now.

I am struggling with this program all day. I made it print something after I add athletes to the disciplines but now it doesn't actually print the values of the athletes. It prints blanks (so it must have taken them from the default constructor i wrote). Moreover rand() gives me some crazy numbers. Now i am trying to make use jonnin's advice and try with <random> but that also gives me some kind of an error. Usually I am a patient person but this is getting me out of my nerves tho when i actually make something work, the feeling is super good.
Don't worry, most of your problems are easy to fix. You're actually making great progress.

The big problem now is that you don't quite understand how class methods work when you call them. When you call a method like someone.getInfo() The object that you called it with (someone in this case) is available to the method. You can just refer to a member of the object to get that member of that object. So getInfo() should look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
	void getInfo() {
		cout << "Enter the info of the Athlete bellow:\n";
		cout << "Athlete's name:  ";
		cin >> name;
		cout << "Athlete's country of origin: ";
		cin >> country;
		cout << endl << "Athlete's best result for the current season: ";
		cin >> yearBest;
		cout << endl << "Athlete's best result for the whole career: ";
		cin >> careerBest;
		cout << endl << "Athlete's number of World Competitions: ";
		cin >> numCompetitions;
	};


Now if you call someone.getInfo(), it will set the name, country, etc. for someone. If you call discipline.athletes[i].getInfo(), it will set the name, country, etc. for discipline.athletes[i].

With this in mind, it should be clear that you don't need to pass anything to Athlete::run(). That method should just set the currentScore of the object that it was called on.

The templates in <random> are much more flexible but a pain to get used to. The good news is that you can just pass one the upper and lower bounds of your range and it will return a value between those values. In your case, the range is 80% of this years best to 110% of the career best (yearBest*0.80 to careerBest*1.10)

I hope this helps.
Yes, it does help to understand how class methods work and how to call them. Sorry for my newbie questions and problems.

Even before your reply, through error and fix, i managed to make it run and actually print my athletes plus random number for the currentScore.

But the number was not actually that random. I was using uniform_real_distribution and with lower bound = 0.8*10 and upper bound = 1.1*12 , i got score of 8.429 (which is okay) but then for the second athlete the lower and upper bounds were 0.8*20 and 1.1*24, which gave me a score of 16.858, which if u look closely is exactly the first score multiplied by two (as my bounds are as well).

Also, i am generating another number with <random> which is normal int and is between 1-100. and if the generated number is equal or smaller of 5, then it is a fault start. The problem here is the same as the problem with the scores. I put a cout in the end of the run() function to see what are the faultStart values that are generated for the different athletes and the scores were one and the same for all athletes ( in that case it was 13)

More over, i guess i have to make different run() functions for the different disciplines as in some discipline lower score is better (such as sprint) and others (like throwing spear) the higher the score, the better. Or more like two run() functions - one for the sports which higher is better and one for the sports which lower is better.

Last edited on
I tried what you said - assuming that you meant competition.athletes[i].getInfo() rather than discipline.athletes[i].getInfo() because it tells me discipline is undefined.

So i made this
1
2
3
4
5
6
7
8
9
10
11
12
void addN()
{
	unsigned NumToAdd = 0;
	cout << "How many athletes do you wish to add?";
	cin >> NumToAdd;

	for (unsigned i = 0; i < competition.size(); i++)
	{
		for (unsigned racer = 0; racer < NumToAdd; ++racer) {
			competition[i].athletes[racer].getInfo();
	}
}


But that is for adding multiple athletes, what about adding single athlete. Of course there again will be the for loop for the disciplines (competition[i]) but then what should be in the brackets for athletes[?]. As if i am not wrong, if i put a number there, it will take the info at this index and overwrite it?
1
2
		for (unsigned racer = 0; racer < NumToAdd; ++racer) {
			competition[i].athletes[racer].getInfo();

This won't quite work. When you say competition[i].athletes[racer], that item doesn't exist in the vector yet. You must add the items to competition[i].athletes with something like push_back().

To fix this, get the info for a dummy instance of Athlete and then use push_back() to add it to the vector:
1
2
3
4
5
		for (unsigned racer = 0; racer < NumToAdd; ++racer) {
			Athlete dummy;
			dummy.getInfo();
			competition[i].athletes.push_back(dummy);
		}


I created the function to add one athlete to each discipline first. It looks almost exactly like your addN():
1
2
3
4
5
6
7
8
9
10
// Add one athlete to each discipline
void addOneEach()
{
    for (unsigned i = 0; i < competition.size(); ++i) {
        cout << "\nEnter Data for athlete in " << competition[i].name << '\n';
        Athlete dummy;
        dummy.getInfo();
        competition[i].athletes.push_back(dummy);
    }
}


Then my addN() just calls addOneEach() N times:
1
2
3
4
5
6
7
8
9
10
11
12
// Add N athletes to each discipline.
void addN()
{
    Athlete dummy;
    unsigned N;
    cout << "How many athletes per discipline would you like to add? ";
    cin >> N;
    for (unsigned j = 0; j<N; ++j) {
        cout << "\n\nATHLETE # " << j+1 << '\n';
        addOneEach();
    }
}


It's a little awkward, but I think this is what the assignment is asking for.

dhayden, a little help please! Problem is that it gives me vector subscript out of range when i try to run 4. Show results (medals, points). So it is about vector<string>countries. But i cant understand how to fix this.

SECOND PART OF CODE IS ON SEPARATE COMMENT DUE TO LENGTH LIMIT

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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231

#include <iostream>
#include <vector>
#include <string>
#include <ctime>
#include <random>
#include <chrono>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;

struct Athlete {
	unsigned id;				// ID number in competition
	string name;
	string country;				// Gotta store it somewhere!
	double careerBest;			// best score of career
	double yearBest;			// best score of this year
	unsigned numCompetitions;	// number of competitions
	double currentScore;		// score for the current
	unsigned medalScore;							// competition.
	string place;
	
	//Prompt user for athlete input
	void getInfo();
	
	// Run a competition and update currentScore
	// There is a 5% change if a fault start. Otherwise
	// the score is a random number between 80% of the yearly
	// best and 110% of career best.
	void run() {
		unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
		std::default_random_engine generator(seed);
		unsigned faultStart;
		std::uniform_int_distribution<> distributionFault(1, 100);
		faultStart = distributionFault(generator);

		if (faultStart <= 5) {
			
			currentScore = -1;
		}
		else
		{
			std::default_random_engine generator(seed);
			std::uniform_real_distribution<> distribution(0.8*yearBest, 1.1*careerBest);
			currentScore = distribution(generator); //+ (careerBest - yearBest);
		}
		cout << faultStart << endl;
	};
	
	void runBigger() {
		unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
		std::default_random_engine generator(seed);
		unsigned faultStart;
		std::uniform_int_distribution<> distributionFault(1, 100);
		faultStart = distributionFault(generator);

		if (faultStart <= 5) {

			currentScore = -1;
		}
		else
		{
			std::default_random_engine generator(seed);
			std::uniform_real_distribution<> distribution(0.9 * careerBest, 1.2 * yearBest);
			currentScore = distribution(generator); //+ (yearBest - careerBest);
		}
		cout << faultStart << endl;
	};
};

vector<string> countries;


struct Discipline {
	string name;				// name of the discipline
	vector<Athlete> athletes;	// the athletes participating in it
	void print();
	void ShowAll(unsigned);
	
};

vector<Discipline> competition;		// The world competition!
//Print results after running competition
/*
void Discipline::print(unsigned a) {
	for (unsigned racer = 0; racer < competition[a].athletes.size(); ++racer) {
		if (competition[a].athletes[racer].currentScore < 0)
		{
			cout << competition[a].athletes[racer].name << " (" << competition[a].athletes[racer].id << ") did a FOUL START\n";
		}
		else
		{
			cout << competition[a].athletes[racer].name << " (" << competition[a].athletes[racer].id << ") scored " <<
				competition[a].athletes[racer].currentScore << " competeing for his country - " <<
				competition[a].athletes[racer].country << endl << endl;
		}
	}
}
*/

void Discipline::print() {
	double result, result1;
	unsigned counter = 0, byPlace, pointsCountry=0;
	string byCountry;
	for (unsigned i = 0; i < 2; ++i)
	{
		for (unsigned j = 0; j < competition[i].athletes.size(); ++j)
		{
			result = competition[i].athletes[j].currentScore;
			for (unsigned k = j + 1; k <= competition[i].athletes.size(); k++)
			{
				result1 = competition[i].athletes[k].currentScore;
				if (result < result1)
				{
					counter++;
				}
			}
			if (counter = (competition[i].athletes.size() - 1)) {
				competition[i].athletes[j].place = "First place";
				competition[i].athletes[j].medalScore = 30;
			}
			else if (counter = (competition[i].athletes.size() - 2)) {
				competition[i].athletes[j].place = "Second place";
				competition[i].athletes[j].medalScore = 20;
			}
			else if (counter = (competition[i].athletes.size() - 3)) {
				competition[i].athletes[j].place = "Third place";
				competition[i].athletes[j].medalScore = 10;
			}
			else if (counter = (competition[i].athletes.size() - 4)) {
				competition[i].athletes[j].place = "Fourth place";
				competition[i].athletes[j].medalScore = 8;
			}
			else if (counter = (competition[i].athletes.size() - 5)) {
				competition[i].athletes[j].place = "Fifth place";
				competition[i].athletes[j].medalScore = 6;
			}
			else {
				competition[i].athletes[j].place = "Sixth place";
				competition[i].athletes[j].medalScore = 4;
			}
			counter = 0;
		}
	}
	for (unsigned i = 2; i < 5; ++i)
	{
		for (unsigned j = 0; j < competition[i].athletes.size(); ++j)
		{
			result = competition[i].athletes[j].currentScore;
			for (unsigned k = j + 1; k <= competition[i].athletes.size(); k++)
			{
				result1 = competition[i].athletes[k].currentScore;
				if (result > result1)
				{
					counter++;
				}
			}
			if (counter = (competition[i].athletes.size() - 1)) {
				competition[i].athletes[j].place = "First place";
				competition[i].athletes[j].medalScore = 30;
			}
			else if (counter = (competition[i].athletes.size() - 2)) {
				competition[i].athletes[j].place = "Second place";
				competition[i].athletes[j].medalScore = 20;
			}
			else if (counter = (competition[i].athletes.size() - 3)) {
				competition[i].athletes[j].place = "Third place";
				competition[i].athletes[j].medalScore = 10;
			}
			else if (counter = (competition[i].athletes.size() - 4)) {
				competition[i].athletes[j].place = "Fourth place";
				competition[i].athletes[j].medalScore = 8;
			}
			else if (counter = (competition[i].athletes.size() - 5)) {
				competition[i].athletes[j].place = "Fifth place";
				competition[i].athletes[j].medalScore = 6;
			}
			else {
				competition[i].athletes[j].place = "Sixth place";
				competition[i].athletes[j].medalScore = 4;
			}
			counter = 0;
		}
	}

	for (unsigned i = 0; i < competition.size(); ++i)
	{
		countries.push_back(competition[i].athletes[0].country);
		for (unsigned j = 1; j < competition[i].athletes.size(); ++j)
		{
			byCountry = competition[i].athletes[j].country;
			for (unsigned k = 0; k <= countries.size(); ++k)
			{
				if (byCountry == countries[k])
					break;
				else if (byCountry != countries[k] || k == countries.size())
					countries.push_back(byCountry);
			}

		}

	}
	for (unsigned i = 0; i < countries.size(); ++i)
	{
		for (unsigned j = 0; j < competition.size(); ++j)
		{
			for (unsigned k = 0; k < competition[j].athletes.size(); ++k)
			{
				if (competition[j].athletes[k].country == countries[i])
				{
					pointsCountry += competition[j].athletes[k].medalScore;
				}
			}
		}
		cout << "Country: " << countries[i] << "; Score: " << pointsCountry
			<< "\n\nAthletes competeing for this country:\n";
		for (unsigned j = 0; j < competition.size(); ++j)
		{
			for (unsigned k = 0; k < competition[j].athletes.size(); ++k)
			{
				if (competition[j].athletes[k].country == countries[i])
					cout << competition[j].athletes[k].name << "----" << competition[j].athletes[k].place
					<< "-----" << competition[j].athletes[k].currentScore << '\n';
			}
		}
		cout << "-------------------------------------------------------\n";
	}
}


second part below!
Last edited on
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153

void Discipline::ShowAll(unsigned a) {
	for (unsigned racer = 0; racer < competition[a].athletes.size(); ++racer) {
			cout << competition[a].athletes[racer].name << " has competition number ->" << competition[a].athletes[racer].id <<" and is competeing in "
				<<competition[a].name<<".\nThe athlete's best result of his career is " <<
				competition[a].athletes[racer].careerBest << " while the best result for the year is " <<
				competition[a].athletes[racer].yearBest<<". "<<competition[a].athletes[racer].name<<" is competeing for "
				<<competition[a].athletes[racer].country <<".\n\n";
		}
	}

void addOneEach() {
	for (unsigned i = 0; i < competition.size(); i++)
	{
		cout << "\nEnter data for athlete competeing in " << competition[i].name << '\n';
		Athlete first;
		first.getInfo();
		competition[i].athletes.push_back(first);
	}
}


void Athlete::getInfo()
{
	cout << "Athlete's name:  ";
	cin >> name;
	cout << "Athlete's country of origin: ";
	cin >> country;
	cout << "Athlete's best result for the current season: ";
	cin >> yearBest;
	cout << "Athlete's best result for the whole career: ";
	cin >> careerBest;
	cout << "Athlete's number of World Competitions: ";
	cin >> numCompetitions;
	cout << endl;
	unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
	std::default_random_engine generator(seed);
	std::uniform_int_distribution<> distributionId(1000, 9999);
	int check;
	id = distributionId(generator);
	for (unsigned l = 0; l < competition.size(); ++l) {
		for (unsigned k = 0; k < competition[l].athletes.size(); ++k) {
			check = competition[l].athletes[k].id;
			if (id==check)
				id = distributionId(generator);
		}
	}
}
// Add athletes to the competition (one per discipline or multiple)
void addN()
{
	Athlete first;
	unsigned NumToAdd;	
		cout << "How many athletes per discipline do you wish to add?";
		cin >> NumToAdd;
		for (unsigned j = 0; j < NumToAdd; ++j) {
			cout << "\n\nAthlete # " << j + 1 << '\n';
			addOneEach();
		}
}


int main()
{
	// initialize the competition
	Discipline races;
	races.name = "Sprint 200m";
	competition.push_back(races);
	races.name = "Relay Race 4x400m";
	competition.push_back(races);
	races.name = "Shot-Put";
	competition.push_back(races);
	//races.name = "Pole Vault";
	//competition.push_back(races);
	//races.name = "Triple Jump";
	//competition.push_back(races);


	int choice, choiceAdd, answer;
	cout << "---------- Welcome to the World Athletics Competition ----------\n\n"
		<< "This program allows you to stre information about athletes as well as\n"
		<< "run a simulation of the World Athletics Competition. You can also see\n"
		<< "post competition statistics for athletes order by different factors!!\n";
	do {

		cout << "---------------------------------------------------------------------\n"
			<< "Choose on of the following options (1-6) :\n"
			<< "1. Add athletes to the system/competition\n"
			<< "2. See all athletes present in the system\n"
			<< "3. Run a simulation of the competition\n"
			<< "4. See results from the competition (medals, points,etc)\n"
			<< "5. See all athletes ordered by age or name\n"
			<< "6. Exit/Quit the program\n";
		cin >> choice;
		switch (choice)
		{
		case 1:
			do {
				cout << "\n1. Add ONE athlete per discipline"
					<< "\n2. Add MULTIPLE athletes per discipline";
				cin >> choiceAdd;
				switch (choiceAdd)
				{
				case 1:
					addOneEach();
					break;
				case 2:
					addN();
					break;
				}
				cout << "\nDo you wish to add more athletes?\n"
					<< "0. No\n"
					<< "1. Yes\n";
				cin >> answer;
			} while (answer != 0);
			break;

		case 2:
			for (unsigned a = 0; a < competition.size(); ++a) {
				cout << "Discipline: " << competition[a].name << endl << endl;
				competition[a].ShowAll(a);
			}
			break;

		case 3:
			for (unsigned i = 0; i < competition.size(); ++i) {
				for (unsigned racer = 0; racer < competition[i].athletes.size(); ++racer) {
					if (i <= 1) {
						competition[i].athletes[racer].runBigger();
					}
					else{
						competition[i].athletes[racer].run();
					}

				}
			}
			break;

		case 4:
			cout << "\n\n---------- COMPETITION RESULTS -------------\n";
			competition[0].print();
			break;

		case 5:

			break;
			

		}
	} while (choice != 6);
	
	return 0;
}
This would be a great time to start using your debugger. Step through your code, to see how it's populating that vectory, and then how it's accessing the elements of the vector. That should show you what's going on.
Turn on your compiler warnings. You'll find a warning near line 121. Here's what I get with g++:
foo.cxx:120:16: warning: suggest parentheses around assignment used as truth va\
lue [-Wparentheses]
    if (counter = (competition[i].athletes.size() - 1)) {
        ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


The problem is that you're using = (which assigns the right side to the left side) instead of == (which compares the left and right sides). You have the same problem throughout Discipline::print().

But let's step back. What is that method trying to do? Sort by country?

Have you learned out to sort data in C++? It seems to me that sorting is important for part E.
Topic archived. No new replies allowed.