I have wrote this code about Soccer score using Data Structures, but it does not give me the Highest Score, Name of the Highest Score achiver and the Table of Players and their scores,points.
As its getting longer and longer i am getting confused. Please help me
#include <iostream>
using namespace std;
int const SIZE = 50; // for char
struct PlayerData
{
int Num;
char Name[SIZE];
int Points;
};
//Function Prototypes
void getPlayerData(PlayerData &); //arguments by ref
int totalTeamPoints(PlayerData [], int);
int highestScore(PlayerData [], int , int &);
int main()
{
cout <<"-------------Soccer Scores---------------"<<endl;
cout<<"\n********************************************"<<endl;
//constatnt for the number of arrays/players
const int Num_Player=3;
int highestPlayer; //the player with highest score
//define a playerData structure variable named info
//and creat an array of playerData structure
PlayerData game[Num_Player];
int highestScore; // the player with highest score
//get the player data for each team
cout<<"Enter the following informations:"<<endl;
for (int i =0; i<Num_Player; i++)
{
//get the info
cout<<"Player"<<(i+1)<<endl;
getPlayerData(game[i]); //by reference
}
// display the total Points
cout<<"\n Total Points-:"
<<totalTeamPoints(game, Num_Player)<<endl;
//Display Highest Score of the Player and the number or name of the player
double highest = highestScore(game, Num_Player, highestPlayer); //highestPlayer is by ref
cout<<"Highest Score:"<<highest;
cout<<"(Player"<<highestPlayer<<"\n";
system("CLS") ;
for (int i=0; i<Num_Player;i++)
{
cout<<game.Num<<"\t\t"<<game.Name<<"\t\t"<<game.points<<"\t\t"<<totalTeamPoints(game, Num_Player);
}
system("PAUSE");
return 0;
}
/* ***********Function to get input about Players *** */
//the getPlayerData Function accepts a PlayerData variable
//it prompts the user for player data and stores the input in the argument
void getPlayerData (PlayerData &data) //a reference paramter
{
//get the player number
cout<<"\tPlayer number-:";
cin>> data.Num;
//validate inputs
while(data.Num<0)
{
cout<<"Error: Number should be positive";
cout<<"\t Player Number-:";
cin>>data.Num;
}
//get the player name
cout<<"\tPlayer Name-:";
cin>>data.Name;
//cin.getline(data.Name);
//get the points
cout<<"\tPoints Scored-:";
cin>>data.Points;
//validate the points
while(data.Points<0)
{
cout<<"Error: Points should be positive";
cout<<"\t Points";
cin>>data.Points;
}
}
/* ***********Function to show total Scores ******** */
// the totalTeamPoints accepts an aray of Palyerdata
//structure and returns the total of all the elements
/* ****************************************** */
int totalTeamPoints(PlayerData data[], int Num_Player)
{
int totalTeamPoints=0; //accumulator
//get the total of members
for (int i =0; i<Num_Player; i++)
totalTeamPoints += data[i].Points;
//return the total
return totalTeamPoints;
}
/* **********Highest Score Function ************ */
// The Highest score accepts palerData array,
//and int indicating the size of array,
//an int by ref holds the player
//with highest score
/* *********************************** */
int highestScore(PlayerData [], int size , int &Player)
{
//set the highest Point to the first player's score
int highest = data[0].Points;
//steps through the array looking for highest
for (int i =0; i<Num_Player; i++)
{
if (data[i].Points>highest)
{
//save this value
highest= data[i].Points;
//save this player number and Name
highest=data[i].Name;
}
}
//return the value
return highest;
}