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;

}

I thought you knew what you were doing until I seen this :

if (data[i].Points>highest) 
      {
         //save this value
         highest= data[i].Points;
         //save this player number and Name
         highest=data[i].Name;
 
       }

What are you trying to achieve here? Explain, so I can see what you
are thinking.

Edited 6 Years Ago by firstPerson: n/a

I am trying to display the name of the player who has scored the most by

#
//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";
#

i want to steps through the array looking for highest points!!!

Do you really this that this code :

if (data[i].Points>highest) 
      {
         //save this value
         highest= data[i].Points;
         //save this player number and Name
         highest=data[i].Name;
 
       }

Does what you think it does, assuming your think the way you comment?

The variable highest is a int variable, but, data.Name is a char array.
Can you assign a int variable a char array?

What made you think that you can " //save this player number and Name" ?

What you wan't to do is this :

int highestScore(PlayerData [], int size , char * PlayerName){

//inside your for loop assign PlayerName to the highest scored points.

//the return highestScore, and PlayerName also contains the player with the highest score.
}

i m overwhlmed now so i cant focus

Thats ok. All you needed to do was a minor change :

int highestScore(PlayerData [], int size , string& playerName)
{
   //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
         playerName=data[i].Name;
 
       }   
 
   }
//return the value
return highest;
 
}

and the call should look like this :

string highestPlayerName = "";

//Display Highest Score of the Player and the number or name of the player
 double highest = highestScore(game, Num_Player, highestPlayerName);  //highestPlayer is by ref
 
  cout<<"Highest Score:"<<highest;
 
  cout<<"(Player"<<highestPlayerName<<"\n";

Edited 6 Years Ago by firstPerson: n/a

This article has been dead for over six months. Start a new discussion instead.