Soccer Scores database. We wrote a program that stores the following information
ID: 3671038 • Letter: S
Question
Soccer Scores database.
We wrote a program that stores the following information of a soccer player in a structure.
1. Players name (make it one string)
2.Players number (make it an integer)
3. Points scored by the player
Use the text file (see below) that has the information of several players in the following format.
Ronaldo 10 12
Messi 1 14
Diego 3 12
Henry 5 12
Kaka 2 6
Alfredo 4 0
Platini 6 16
We wrote a function
Player* file2Players(string filename,int &numPlayers)
that will read this file and returns the pointer pointing to the dynamically allocated array of structures of players.
We also wrote a function that accepts two arguments - an array of structures of soccer players and size of the array and displays the player details in the array.
void DisplayPlayers(Player *playerPtr, int numPlayers)
Now write a function that accepts two arguments – a pointer to the array of structure of soccer players and number of players. The function should sort the array of structure of soccer players based on descending order of scores of the players. After sorting, display the information of the sorted players.
void sortPlayers(Player *playerPtr, int numPlayers)
This is part of the problem(can you help me to finish it ?)
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct Player
{
string name; //player's name
int shirt;
int score;
};
const int numPlayers = 7; // numbers of players
Player* file2Players(string filename,int &numPlayers)
{
ifstream in;
in.open(filename);
string line;
numPlayers = 0;
while(getline(in,line)) // in>>line
{numPlayers++;
}
in.close();
Player *playerPtr = new Player[numPlayers];//dynamic allocation
//playerPtr[0]//first player
//playerPtr[1]//second player
in.open(filename);
int count = 0;
bool Exist = true;
while(in>>playerPtr[count].name)
{
in>>playerPtr[count].shirt;
in>>playerPtr[count].score;
count++;
}
in.close();
return playerPtr;
}
void sortPlayers(Player *playerPtr, int numPlayers)
{
}
void DisplayPlayers(Player *playerPtr, int numPlayers)
{
cout<<" Player Database ";
for(int i = 0 ; i < numPlayers ; i++)
{
cout<<playerPtr[i].name<<" "<<playerPtr[i].shirt<<" " <<playerPtr[i].score<<endl;
}
}
int main()
{ int numPlayers;
Player *playerPtr = file2Players("Text.txt",numPlayers);
cout<<numPlayers<<endl;
DisplayPlayers(playerPtr,numPlayers);
system("pause");
return 0;
}
Explanation / Answer
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
const int SIZE = 50;
int TotalPoints(int *, int);
struct Player
{
char name[SIZE]; //Player's Name
int playNum; //Player's Number
int Points; //Point's Scored
};
int main()
{
const int NUM_PLAYERS = 12; //Number of Players
// Dynamically allocate the memory needed.
Player *players = new Player[NUM_PLAYERS]; //Array of structures
int index; //Loop
int total=0;
// Get Player data.
cout << " You will need the following information. ";
cout << "Pertaining to your Soccer Players. ";
cout << "The Player's Names, Player's Numbers ";
cout << "Finally you will need the Points Scored by Players. ";
for (index = 0; index < NUM_PLAYERS; index++)
{
cout << "Please enter the Player's Name: ";
cin.getline( players[index].name, 50 );
cout << "Please enter the Player's Number: ";
( cin >> players[index].playNum ).get();
//To test my values for zero, negative
while (players[index].playNum <=0)
{
cout << "Zero or negative numbers not allowed ";
cout << "Please enter the Player's Number: ";
(cin >> players[index].playNum).get();
}
cout << "Please enter the Points Scored by the Player: ";
( cin >> players[index].Points ).get();
//To test my values for zero, negative.
while (players[index].Points < 0)
{
cout << "Zero or negative numbers not allowed ";
cout << "Please enter the Points Scored by the Player: ";
(cin >> players[index].Points).get();
}
cout << endl << endl;
}
//Display the players data
cout << "Here is the players data: ";
cout << " Name Number Score ";
cout << "-------------------------------- ";
for (index = 0; index < NUM_PLAYERS; index++)
{
cout << setw(8) << players[index].name;
cout << setw(8) << players[index].playNum;
cout << setw(8) << players[index].Points << endl;
}
//Calculate the total points
for (index = 0; index < NUM_PLAYERS; index++)
{
total += players[index].Points;
}
//Display the results of the total points.
cout << " The total of points scored by the team are: ";
cout << total << endl;
//To get the player with most points
int max = players[0].Points;
int maxIndex = 0;
for (int index = 0; index < 12; index++)
{
if (players[index].Points > max)
{
max = players[index].Points;
maxIndex = index;
}
}
cout << "highest score by: <" << players[maxIndex].name << "> number: " << players[maxIndex].playNum << endl;
cin.get();
// Delete the memory.
delete [] players;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.