In: Computer Science
Write a C++ program to help the Administration of a football league to manipulate the list of players registered in different teams. There are 26 teams participating in the league, each is denoted by a letter in the range A to Z. Each team can have 11 players at most. The information of all teams' players are stored in a text file named 'players.dat'. Each line from the input file contains the details of one player; where the player's details include his first-name and family- name, the age (in years), and the team's letter (from the range A-Z). --see the sample input below
The intended program will perform the following tasks:
Continuously displaying a menu for the user until the user selects 'X' (or 'x') to exit the program. The menu provides 4 options: Add Player (A or a), Team Inquiry (I or i), Print Statistics (P or p), and Exit (X or x).
When the 'Add Player' operation is selected, the main function will ask the user to provide all the information of the new player, then it calls a user-defined function named addPlayer(...) to add the player to the file. Before the record is added to the file, two things must be checked:
o The provided team-letter must be in the range A-Z. This must be validated via another user- defined function named teamCodeIsValid(...); and a suitable error message should be displayed by the function addPlayer(...) if the letter is invalid.
o The number of the players in the specified team must be less than 11 before adding the new player. This must be counted and returned via another user-define function named countPlayers(...); and a suitable error message should be displayed if the team is already full.
When the 'Team Inquiry' operation is selected, the main function should ask the user to provide the letter of the desired team, then it calls a user-defined function (named displayTeam(...)) to display the details and the number of all players in the team. If the specified team has no players at all, then the function displayTeam(...) should display a suitable notification to the user.
When the 'Print Statistics' operation is selected, the main function will call a user-defined function named printStat(...) to display a statistical report which shows the number of players in each team (see sample output). HINT: CONSIDER USING THE FUNCTION countPlayers(...).
please refer below code
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
//Checkin number of players in team if 11 return false else
true
bool countPlayers(int *team_count,char ch)
{
if(team_count[ch - 'A'] < 11)
return true;
return false;
}
//checking valid team code
bool teamCodeIsValid(char ch)
{
return ch >= 'A' && ch <= 'Z';
}
//adding player in the file
void addPlayer(string name, string surname, int age, char
team_name, ofstream &outfile, int *team_count)
{
if (!teamCodeIsValid(team_name))
{
cout<<"\nInvalid team Name"<<endl;
}
else if(!countPlayers(team_count,team_name))
{
cout<<"\nteam is full"<<endl;
}
else
{
outfile << name <<" "<< surname <<"
"<< age << " " <<team_name<<endl;
team_count[team_name - 'A']++;
}
}
//displaying the team stats
void displayTeam(char *file_name,char team_name, int
team_count)
{
string name,surname;
int age;
char tn,ch;
std::ifstream infile;
infile.open(file_name);
if(team_count == 0)
{
cout<<"No players at all in the team"<<endl;
return;
}
//reading output file created
while(infile >> name >> surname >> age >>
tn)
{
if(tn - team_name == 0)
cout<<name<<" "<<surname<<"
"<<age<<" "<<team_name<<endl;
}
cout<<"Total Number of players =
"<<team_count<<endl;
}
//printing stats
void printStat(int *team_count)
{
char ch = 'A';
cout<<"Team Name\tNumber of Players"<<endl;
for(int i = 0; i < 26; i++)
{
cout<<ch<<"\t\t"<<team_count[i]<<endl;
ch++;
}
}
int main()
{
char option,team_name;
string name,surname;
int age;
std::ofstream outfile;
int team_count[26] = {0};
char file_name[20] = "players.dat";
outfile.open(file_name);
while(1)
{
cout<<"\nMenu : "<<endl;
cout<<"Add Player [A or a]\nTeam Inquiry [I or i]\nPrint
Statistic [P or p]\nExit [X or x]"<<endl<<endl;
cin>>option;
if(option == 'A' || option == 'a')
{
cout<<"Enter Name of the player : ";
cin>>name;
cout<<"\nEnter Family name of the player : ";
cin>>surname;
cout<<"\nEnter age of the Player : ";
cin>>age;
cout<<"\nEnter team name : ";
cin>>team_name;
addPlayer(name,surname,age,team_name,outfile,team_count);
}
else if(option == 'I' || option == 'i')
{
cout<<"\nEnter the team name for Inquiry : ";
cin>>team_name;
displayTeam(file_name,team_name,team_count[team_name-'A']);
}
else if(option == 'P' || option == 'p')
{
cout<<"\nBelow are the statistics of team"<<endl;
printStat(team_count);
}
else
{
cout<<"\nExiting....."<<endl;
break;
}
}
return 0;
}
please refer below output
Menu :
Add Player [A or a]
Team Inquiry [I or i]
Print Statistic [P or p]
Exit [X or x]
A
Enter Name of the player : Shane
Enter Family name of the player : Warne
Enter age of the Player : 25
Enter team name : A
Menu :
Add Player [A or a]
Team Inquiry [I or i]
Print Statistic [P or p]
Exit [X or x]
A
Enter Name of the player : Ricky
Enter Family name of the player : Ponting
Enter age of the Player : 32
Enter team name : B
Menu :
Add Player [A or a]
Team Inquiry [I or i]
Print Statistic [P or p]
Exit [X or x]
A
Enter Name of the player : Chris
Enter Family name of the player : Martin
Enter age of the Player : 31
Enter team name : B
Menu :
Add Player [A or a]
Team Inquiry [I or i]
Print Statistic [P or p]
Exit [X or x]
I
Enter the team name for Inquiry : B
Ricky Ponting 32 B
Chris Martin 31 B
Total Number of players = 2
Menu :
Add Player [A or a]
Team Inquiry [I or i]
Print Statistic [P or p]
Exit [X or x]
P
Below are the statistics of team
Team Name Number of Players
A 1
B 2
C 0
D 0
E 0
F 0
G 0
H 0
I 0
J 0
K 0
L 0
M 0
N 0
O 0
P 0
Q 0
R 0
S 0
T 0
U 0
V 0
W 0
X 0
Y 0
Z 0
Menu :
Add Player [A or a]
Team Inquiry [I or i]
Print Statistic [P or p]
Exit [X or x]
X
Exiting.....
Process returned 0 (0x0) execution time : 67.546 s
Press any key to continue.