Soccer Scores database. Write a program that stores the following information of
ID: 3671332 • Letter: S
Question
Soccer Scores database.
Write 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 below:
Ronaldo 10 12
Messi 1 14
Diego 3 12
Henry 5 12
Kaka 2 6
Alfredo 4 0
Platini 6 16
write 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.
Also write 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)
Finally 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)
Explanation / Answer
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
const int SIZE = 50;
struct Player
{
char name[SIZE]; //Player's Name
int playNum; //Player's Number
int Points; //Point's Scored
};
const int NUM_PLAYERS = 3; //Number of Players
// Dynamically allocate the memory needed.
Player *players = new Player[NUM_PLAYERS]; //Array of structures
int total = 0;
void getPlayerInfo(Player &);
void showInfo(Player);
int getTotalPoints(Player[], int);
void showHighest(Player[], int);
int main()
{
getPlayerInfo(*players);
getTotalPoints(players,total);
showInfo(*players);
}
void getPlayerInfo(Player&)
{
int index;
// 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;
}
return;
}
int getTotalPoints(Player[], int)
{
int index;
int total = 0;
//Calculate the total points
for (index = 0; index < NUM_PLAYERS; index++)
{
total += players[index].Points;
}
int TotalPoints(int *, int);
return total;
}
void showInfo(Player)
{
int TotalPoints = 0;
int index = 0;
//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;
}
//Display the results of the total points.
cout << " The total of points scored by the team are: ";
cout << TotalPoints << 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;
return;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.