You are on page 1of 4

SUMMARY

This program allows you to enter data for 12 players and in


return, it
summarizes all the player data, calculates the total points
scored by
the entire team, and lists all players who acheived the
highest score.

INPUT

This program accepts input from Standard Input, either


interactively
or piped in from a data file. The data file should be line-
delimited.

OUTPUT

All output is sent to Standard Output, including error


messages. A
table is outputted in this format:

Name Uniform # Score


-------------------------------------------------
Dave Lambert 144 870
Rob Smith 140 500
Bob Saget 92 108
Joan Rivers 55 603
Jimmy Hawthorne 23 870
Bobby Boate 22 324
Gale Wind 30 456
Thirsty Waters 20 134
Thurston Howell III 19 870
Tom Petty 59 321
Harry Haret 54 72
Blonde Brunette 12 45

DATA STRUCTURES

One data structure is used in this program: struct Player.


It
consists of the player's information: a string and two
integers. An
array of 12 of these structs is used to store the team's
data.

ASSUMPTIONS

This program assumes input from standard input and outputs


to standard
output. When asked for a number, the program assumes a
number of the
correct data type will be entered.

*/

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;


struct Player {
string playerName; // Player's name
int playerNumber; // Player's uniform number
int playerPoints; // Player's points earned
};

typedef struct Player Player;

void setPlayerInfo(Player play[]);


void printSummary(Player play[]);
void printTotalPts(Player play[]);
void printHighScores(Player play[]);

const int numPlayers = 12; // Total # of players on the team

int main() { // main

/* The main() function... Everything


* is pretty straightforward: using loops, reading and writing to and
from
* standard input/output, usage of the above struct, etc.
*
*/

// Array of Player structs


Player *p = new Player[numPlayers];
//Player p[numPlayers];

cout << "----------------------------------------\n";


cout << " Soccer Score Utility\n";
cout << "----------------------------------------\n";

cout << "\nPlease enter player info for " << numPlayers << "
players:\n\n";

setPlayerInfo(&p[numPlayers]);

printSummary(&p[numPlayers]);

printTotalPts(&p[numPlayers]);

printHighScores(&p[numPlayers]);

cout << "\nProgram terminated successfully.\n";

return 0;
}

void setPlayerInfo(Player play[]) {


// Loop to read player info into array
for (int i = 0; i < numPlayers; i++) {
cout << "PLAYER #" << i + 1; // Make list appear to start
at 1
cout << "\n----------\n";
// Read Player Name from STDIN
cout << "Player Name: ";
getline(cin, play[i].playerName);
cout << play[i].playerName; // echoprint

// Read Player Number from STDIN


while (true) { // Infinite loop
cout << "\nUniform Number: ";
(cin >> play[i].playerNumber).get();
cout << play[i].playerNumber; // echoprint
// Don't continue with a negative number
if (play[i].playerNumber < 0)
cout << "\nNegative numbers not allowed. Try
again!";
else
break;
} // Infinite loop

// Read Player's Points from STDIN


while (true) { // Infinite loop
cout << "\nPoints Scored: ";
(cin >> play[i].playerPoints).get();
cout << play[i].playerPoints; // echoprint
// Don't continue with a negative number
if (play[i].playerPoints < 0)
cout << "\nNegative numbers not allowed. Try
again!";
else
break;
} // Infinite loop

cout << "\n\n";


} // Loop to read player info into array
}

void printSummary(Player play[]) {

// Print summary of player info data


cout << "\nPlayer Summary\n\n";
cout << left << setw(30) << "Name" << setw(12) << "Uniform #" <<
setw(12)
<< "Score" << "\n"; // format
table headers
cout << "-------------------------------------------------\n";
// Loop to print player info summary
for (int i = 0; i < numPlayers; i++) {
cout << setw(30) << play[i].playerName;
cout << setw(12) << play[i].playerNumber;
cout << setw(12) << play[i].playerPoints << "\n";
}
}

void printTotalPts(Player play[]) {

int totalPoints = 0; // total points earned by the entire


team

// Loop to print total points earned by team


for (int i = 0; i < numPlayers; i++) {
totalPoints += play[i].playerPoints;
}
cout << "\nTotal points earned by entire team: " << totalPoints <<
"\n\n";
}

void printHighScores(Player play[]) {


int highScore = 0; // highest score achieved by
any player

// Print player with most earned points


// Loop to first find what the highest score is
for (int i = 0; i < numPlayers; i++) {
if (play[i].playerPoints > highScore) {
highScore = play[i].playerPoints;
}
}

cout << "Player(s) who ranked First Place:\n";


// Loop to print all players that have the highest score
for (int i = 0; i < numPlayers; i++) {
if (play[i].playerPoints == highScore) {
cout << "#" << play[i].playerNumber << " " <<
play[i].playerName << "\n";
}
}
}

You might also like