Write a program that stores the following data about a soccer player in a struct
ID: 3907091 • Letter: W
Question
Write a program that stores the following data about a soccer player in a structure:
Player’s Name as String
Player’s Number as Short Int (or char)
Points Scored by Player as Short Int
The program should keep an array of 20 of these structures. Each element is for a different
player on a team. When the program runs it should ask the user to enter the data
for each player. It should then show a table that lists each player’s number, name, and
points scored. The program should also calculate and display the total points earned by
the team. The number and name of the player who has earned the most points should
also be displayed.
Input Validation: Do not accept negative values for players’ numbers or points scored.
Program must also:
Use pointers for your array of structures with dereferencing (ie. *(array_name + index) technique) instead of [ ] operator.
Ask the user if they would like to sort array by name, player number or total points scored.
Write the array of structures to a text file (ask the user to enter a file name for the file to output).
Write the array of structures to a binary file (ask the user to enter a file name for the file to output).
Write a second program to read in a text or binary file from items #3 or #4, using binary search to find a player by name and output the player's information (name, number, and total points)
Explanation / Answer
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
const int MAX_ SIZE = 50;
struct SoccerPlayer
{
char name[MAX_ SIZE]; //SoccerPlayer's Name
int playNum; //SoccerPlayer's Number
int Points; //Point's Scored
}record[20];
const int NUM_SOCCER_PLAYERS = 20; //Number of soccerPlayers
SoccerPlayer *soccerPlayers = new SoccerPlayer[NUM_SOCCER_PLAYERS]; //Array of structures
int total = 0;
void getSoccerPlayerInfo(SoccerPlayer &);
void showInformation(SoccerPlayer);
int getTotalPointsOfPlayers(SoccerPlayer[], int);
void showHighest(SoccerPlayer[], int);
int main()
{
getSoccerPlayerInfo(*soccerPlayers);
getTotalPointsOfPlayers(soccerPlayers,total);
showInformation(*soccerPlayers);
sorting();
}
void getSoccerPlayerInfo(SoccerPlayer&)
{
int position;
// Get SoccerPlayer data.
cout << " You will need the following information. ";
cout << "Pertaining to your Soccer soccerPlayers. ";
cout << "The SoccerPlayer's Names, SoccerPlayer's Numbers ";
cout << "Finally you will need the Points Scored by soccerPlayers. ";
for (position = 0; position < NUM_SOCCER_PLAYERS; position++)
{
cout << "Please enter the SoccerPlayer's Name: ";
cin.getline(soccerPlayers[position].name, 50);
cout << "Please enter the SoccerPlayer's Number: ";
(cin >> soccerPlayers[position].playNum).get();
//To test my values for zero, negative
while (soccerPlayers[position].playNum <= 0)
{
cout << "Zero or negative numbers not allowed ";
cout << "Please enter the SoccerPlayer's Number: ";
(cin >> soccerPlayers[position].playNum).get();
}
cout << "Please enter the Points Scored by the SoccerPlayer: ";
(cin >> soccerPlayers[position].Points).get();
//To test my values for zero, negative.
while (soccerPlayers[position].Points < 0)
{
cout << "Zero or negative numbers not allowed ";
cout << "Please enter the Points Scored by the SoccerPlayer: ";
(cin >> soccerPlayers[position].Points).get();
}
cout << endl << endl;
}
return;
}
int getTotalPointsOfPlayers(SoccerPlayer[], int)
{
int position;
int total = 0;
//Calculate the total points
for (position = 0; position < NUM_SOCCER_PLAYERS; position++)
{
total += soccerPlayers[position].Points;
}
int TotalPoints(int *, int);
return total;
}
void showInformation(SoccerPlayer)
{
int TotalPoints = 0;
int position = 0;
//Display the soccerPlayers data
cout << "Here is the soccerPlayers data: ";
cout << " Name Number Score ";
cout << "-------------------------------- ";
for (position = 0; position < NUM_SOCCER_PLAYERS; position++)
{
cout << setw(8) << soccerPlayers[position].name;
cout << setw(8) << soccerPlayers[position].playNum;
cout << setw(8) << soccerPlayers[position].Points << endl;
}
//Display the results of the total points.
cout << " The total of points scored by the team are: ";
cout << TotalPoints << endl;
//To get the SoccerPlayer with most points
int max = soccerPlayers[0].Points;
int maxposition = 0;
for (int position = 0; position < 20; position++)
{
if (soccerPlayers[position].Points > max)
{
max = soccerPlayers[position].Points;
maxposition = position;
}
}
cout << "highest score by: " << soccerPlayers[maxposition].name << " number: " << soccerPlayers[maxposition].playNum << endl;
return;
}
void sorting(){
int options;
cout << "Please enter 1 to sort by name, 2 to sort by player number or 3 to sort by total points ";
cin >> options;
// sort by name
bool sortByName(const SoccerPlayer &lhs, const SoccerPlayer &rhs ){
return lhs.name < rhs.name;
}
// sort by name
bool sortByPlayerNumber(const SoccerPlayer &lhs, const SoccerPlayer &rhs ){
return lhs.playNum < rhs.playNum;
}
// sort by total points
bool sortByTotalPoint(const SoccerPlayer &lhs, const SoccerPlayer &rhs ){
return lhs.Points < rhs.Points;
}
if(options == 1){
sort(soccerPlayers.begin(), soccerPlayers.end(), sortByName);
for(SoccerPlayer &n : soccerPlayers)
cout << n.name << " ";
cout << n.playnum << " ";
cout << n.Points << " ";
cout << endl;
} else if(options == 2){
sort(soccerPlayers.begin(), soccerPlayers.end(), sortByPlayerNumber);
for(SoccerPlayer &n : soccerPlayers)
cout << n.name << " ";
cout << n.playnum << " ";
cout << n.Points << " ";
cout << endl;
} else if(options == 3){
sort(soccerPlayers.begin(), soccerPlayers.end(), sortByTotalPoints);
for(SoccerPlayer &n : soccerPlayers)
cout << n.name << " ";
cout << n.playnum << " ";
cout << n.Points << " ";
cout << endl;
} else {
cout<< "Invalid options";
}
}
void readToTextFile(){
int position,no_of_players=20;
string filePath;
ifstream inFile;
cout<<"Enter full path name of file to save to"<<endl; //Ask user to define file name to save to
cin>> filePath; //user input for file path name
inFile.open (filePath);
if (!inFile)
{
cout<<"Cannot load file"<<endl;
return ;
}
else
{
for (i=0; i < no_of_players; i++){
inFile>>
&record[i].name,
&record[i].playNum,
&record[i].Points;
cout<<" Player number "<<i<<"stored"<<endl;
}
cout <<"All members have been successfully loaded"<<endl;
inFile.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.