In: Computer Science
C++ Chapter 4/5 Lab Assignment Using concepts from chapters 1 – 5 only. Grading will be based on chapter 4 concepts more. Design a menu driven program that can keep track of five player’s scores. Your program must have the following documentation: A. Your name B. The program name C. Program Description D. The date the exe file was created E. The code: a. Use a menu approach do the following: i. to Add a player information ii. to Search for any player based on their name. iii. to Display all the information at any time (Hint : Use value and reference parameters as necessary).. b. Organize the main program to call input and output functions. Use static variables to keep track of player’s information. c. Input the names of five players and their highest scores on three games they ever played. Do Not Hard-Code the players’ names and their scores. d. Create an average function to compute the average highest score of each player i.e. ( john: g1 100 g2 200 g3 300. Average highest score will be 200.00).
Source code:
#include <iostream>
using namespace std;
/*
* class to store player details
*/
class Player{
string name; //name of the player
int scores[3]; //highest scores of 3 matches
public:
Player(){} //default constructor
//constructor to initialize details
Player(string name, int s0, int s1, int s2){
this->name = name;
scores[0] = s0;
scores[1] = s1;
scores[2] = s2;
}
//to get name
string getName(){
return name;
}
//to get score of match i
int getScore(int i){
return scores[i];
}
};
void getPlayerDetails(Player players[], int*
total_players);
void searchAPlayer(Player players[], int total_players);
void displayDetailsOfAllPlayers(Player players[], int
total_players);
void displayAverageScores(Player players[], int
total_players);
double findAverageScore(Player players[], int indx);
int main(){
//player object to store details of 5 players
Player players[5];
int total_players = 0; //total players
char ch;
//loop until user exits
do{
//main menu display
cout << "1. Enter the details of a player" <<
endl
<< "2. Search for a player" << endl
<< "3. Display the details of all players" <<
endl
<< "4. Display average highest scores of all players"
<< endl
<< "0. Exit" << endl
<< "Your choice: ";
cin >> ch;
switch(ch){
case '1':
//choice to add player details
getPlayerDetails(players, &total_players);
break;
case '2':
//choice to search for a player
searchAPlayer(players, total_players);
break;
case '3':
//choice to display details of all players
displayDetailsOfAllPlayers(players, total_players);
break;
case '4':
//choice to display average scores
displayAverageScores(players, total_players);
break;
case '0':
//choice to exit
break;
default:
//invalid choice
cout << "Invalid choice" << endl << endl;
break;
}
}while(ch != '0');
return 0;
}
/*
* get details of a player
*/
void getPlayerDetails(Player players[], int* total_players){
string name;
int s0, s1, s2;
cout << "Enter name of the player: ";
cin >> name;
cout << "Enter the score 1 : ";
cin >> s0;
cout << "Enter the score 2 : ";
cin >> s1;
cout << "Enter the score 3 : ";
cin >> s2;
cout << endl;
players[*total_players] = Player(name, s0, s1, s2);
*total_players = *total_players + 1;
}
/*
* search for a player
*/
void searchAPlayer(Player players[], int total_players){
string name;
cout << "Enter name of the player to search: ";
cin >> name;
for(int i = 0; i < total_players; i++){
if(players[i].getName().compare(name) == 0){
cout << "The details of player are:" << endl;
cout << "\tScore 1: " << players[i].getScore(0)
<< endl;
cout << "\tScore 2: " << players[i].getScore(1)
<< endl;
cout << "\tScore 3: " << players[i].getScore(2)
<< endl << endl;
return;
}
}
cout << "Player details not found." << endl <<
endl;
}
/*
* display details of all players
*/
void displayDetailsOfAllPlayers(Player players[], int
total_players){
for(int i = 0; i < total_players; i++){
cout << "Player " << i + 1 << " name : " <<
players[i].getName() << endl;
cout << "\tscore 1 : " << players[i].getScore(0)
<< endl;
cout << "\tscore 2 : " << players[i].getScore(1)
<< endl;
cout << "\tscore 3 : " << players[i].getScore(2)
<< endl;
}
cout << endl;
}
/*
* display average scores
*/
void displayAverageScores(Player players[], int
total_players){
for(int i = 0; i < total_players; i++){
cout << "Player name : " << players[i].getName()
<< endl;
cout << "Average score: " << findAverageScore(players,
i) << endl << endl;
}
cout << endl;
}
/*
* find average score of a player
*/
double findAverageScore(Player players[], int indx){
double total_score = 0.0;
for(int i = 0; i < 3; i++){
total_score += players[indx].getScore(i);
}
return total_score / 3;
}
Code screenshot:
Output: