-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSoccerScores.cpp
139 lines (114 loc) · 3.46 KB
/
SoccerScores.cpp
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
// Chapter 11, Programming Challenge 6
// Soccer Scores
#include <iostream>
#include <iomanip>
using namespace std;
// Constant for name array size
const int SIZE = 45;
// Constant for the number of players
const int NUM_PLAYERS = 4;
int i = 0;
// Declaration of the Player structure
struct Player
{
char name[SIZE];
int number;
int points;
};
// Function prototypes
void getPlayerInfo(Player &);
void showInfo(Player);
int getTotalPoints(Player[], int);
void showHighest(Player[], int);
//***********************************************
// Function main *
//***********************************************
int main()
{
// Array of Player structures
Player team[NUM_PLAYERS];
// Loop counter
int index;
// Get each player's info.
for (index = 0; index < NUM_PLAYERS; index++)
getPlayerInfo(team[index]);
// Display the table headings.
cout << setw(20) << left << "\nNAME";
cout << setw(10) << "NUMBER";
cout << setw(10) << "POINTS SCORED\n";
// Display the team info.
for (index = 0; index < NUM_PLAYERS; index++)
showInfo(team[index]);
// Display total points
int total = 0;
for (index = 0; index < NUM_PLAYERS; index++)
{
total += getTotalPoints(team, index);
}
cout << setw(20) << left << "TOTAL POINTS: " << total << endl;
// Display the player scoring the most points.
showHighest(team, NUM_PLAYERS);
return 0;
}
//***********************************************
// Function getPlayer *
// This function accepts a reference to a Player*
// structure variable. The user is asked to *
// enter the player's name, number, and the *
// number of points scored. This data is stored *
// in the reference parameter. *
//***********************************************
void getPlayerInfo(Player &p)
{
cout << "PLAYER #" << (i+1) << endl;
cout << "--------\n\n";
cout << "Player name: ";
cin.getline(p.name, SIZE);
cout << "Player's number: ";
cin >> p.number;
cout << "Point scored: ";
cin >> p.points;
cout << "\n";
cin.ignore();
i++;
}
//***********************************************
// Function showInfo *
// This function displays the data in the Player*
// structure variable passed into the parameter.*
//***********************************************
void showInfo(Player p)
{
cout << setw(20) << left << p.name;
cout << setw(10) << p.number;
cout << setw(10) << p.points << "\n";
}
//***********************************************
// Function getTotalPoints *
// This function accepts an array of Player *
// structure variables as its argument. The *
// function calciulates and returns the total *
// of all the players points in the array. *
//***********************************************
int getTotalPoints(Player p[], int size)
{
return p[size].points;
}
//***********************************************
// Function showHighest *
// This function accepts an array of Player *
// structure variables. It displays the name *
// of the player who scored the most points. *
//***********************************************
void showHighest(Player p[], int size)
{
int highest;
highest = p[0].points;
for (int count = 1; count < size; count++)
if (highest < p[count].points)
highest = p[count].points;
for (int count = 1; count < size; count++)
if (highest == p[count].points)
cout << "The player who scored the most points is: "
<< p[count].name << endl;
}