I'm supposed to write a program that stores data about a soccer player in a structure: Players name, players number, points scored by the player. The program should keep an array of 12 structures. Each element for a differnet player on the team. When the program runs it should ask for the players data. Then return the data in a table, it should calculate and show total points. and finaly display the player with the most poinst to show his number and name. input validation, Not to accept negative values for numer and points.


This is what i have so far:

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

const int SIZE = 50;
int TotalPoints(int *, int);

struct Players
{
       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.
    Players *players = new Players[NUM_PLAYERS];      //Array of structures
    int index;                 //Loop

    // Get Player data.
    cout << "\nYou will need the following information.\n";
	cout << "Pertaining to your Soccer Players.\n";
    cout << "The Player's Names, Player's Numbers\n";
	cout << "Finally you will need the Points Scored by Players.\n\n\n";
    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\n";
			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\n";
			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:\n\n";
	cout << "    Name    Number    Score	\n";
	cout << "--------------------------------\n";

	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;
	}


	//Displays the total number of points scored by the team.
	cout << "The total points scored by the team is: ";
	cout << TotalPoints(Points, NUM_PLAYERS) << endl;

	// Delete the memory.
	delete [] players;

    return 0;

}



int TotalPoints(int (players[index].(*Points)), int NUM_PLAYERS)
{
	int Total = 0.0;

	for (int players[index].Points; players[index].Points < NUM_PLAYERS; index++)
	{
		sum += (players[index].(*Points));
		Points++;
	}
	return Total;
}

While trying to get the total points, i keep on failing every time. What am i doing wrong. I get half of my errors from undeclared identifiers from Points, players, index and other for lots of missing brackets parenthesis in Total points last section. If i remove the totals, the program works, so hopefully im not that far off.

Also like how do you make a program return two pieces of data from the same player his name and number. I was thinking of trying to use the get highest, but how do u make it return not the points but the other info about the soccer player.

Also like how do you make a program return two pieces of data from the same player his name and number. I was thinking of trying to use the get highest, but how do u make it return not the points but the other info about the soccer player.

Have it return the entire Player, then pick the two pieces of information you want from that Player. That'd be the easiest way.

This function here looks quite odd to me. Line 1 looks more like a function CALL:

int TotalPoints(int (players[index].(*Points)), int NUM_PLAYERS)
{
	int Total = 0.0;

	for (int players[index].Points; players[index].Points < NUM_PLAYERS; index++)
	{
		sum += (players[index].(*Points));
		Points++;
	}
	return Total;
}

Line 1 - You want something like : int TotalPoints(Players* players, int NUM_PLAYERS) , I would imagine.
Line 2 - If Total is an integer, leave off the decimal part.
Line 4 - Why are you comparing Points to the number of players? If you are looping through an array, just have the variable be index, not players[index].Points .

I would rename your struct Player instead of Players . It only represents a single player, right?

Does TotalPoints need to be a function?

If not, then just run through the array summing up everyone's score.

int main()
{
....
	int sum = 0;
	for (int x_var = 0; x_var <NUM_PLAYERS ;x++)
		sum += players[x_var];
...
}

I tried using both ways in the code, and in both i found a common error. += no global operator. Below is how i attempted to write it inside the main body.

// To add up total points
	int sum =0;
	for (int index = 0; index < NUM_PLAYERS; index++)
	{
		sum += players[index];
		index++;

	cout << "The total points scored by the team is: ";
	cout << sum << endl;
	}

This is by using the function method, i like to use this method because its the one i need more practice with. But I don't think it matters.

//Displays the total number of points scored by the team.
	cout << "The total points scored by the team is: ";
	cout << TotalPoints(Players* players, NUM_PLAYERS) << endl;

	// Delete the memory.
	delete [] players;

    return 0;

}

int TotalPoints(Players* players, int NUM_PLAYERS)
{
	int sum = 0;

	for (int index; index < NUM_PLAYERS; index++)
	{
		sum += players[index];
		index++;
	}
	return sum;
}

With this one i get Players illegal use and see declaration and the += no global. I took the advice from vernon but to me it doesn't seem right to just leave line 4 0r5 on his post just as index . How does the program know which one of the index to use because i got them for name, number and points. And right now i want it to just add up all the points from all the players. Or am i just overthinking the entire situation.

I tried using both ways in the code, and in both i found a common error. += no global operator. Below is how i attempted to write it inside the main body.

// To add up total points
	int sum =0;
	for (int index = 0; index < NUM_PLAYERS; index++)
	{
		sum += players[index];
		index++;

	cout << "The total points scored by the team is: ";
	cout << sum << endl;
	}

This is by using the function method, i like to use this method because its the one i need more practice with. But I don't think it matters.

//Displays the total number of points scored by the team.
	cout << "The total points scored by the team is: ";
	cout << TotalPoints(Players* players, NUM_PLAYERS) << endl;

	// Delete the memory.
	delete [] players;

    return 0;

}

int TotalPoints(Players* players, int NUM_PLAYERS)
{
	int sum = 0;

	for (int index; index < NUM_PLAYERS; index++)
	{
		sum += players[index];
		index++;
	}
	return sum;
}

With this one i get Players illegal use and see declaration and the += no global. I took the advice from vernon but to me it doesn't seem right to just leave line 4 0r5 on his post just as index . How does the program know which one of the index to use because i got them for name, number and points. And right now i want it to just add up all the points from all the players. Or am i just overthinking the entire situation.

+= doesn't exist for a struct unless YOU define one. You want to isolate an integer and and add IT, not add a struct. It's a two-step process. First step, isolate a single Player from the array. Second step, isolate a particular part of that Player, Points in this case.

You're not overthinking; you are unclear on the difference between accessing an object of type Players and acessing something from WITHIN a Players object. I am going to change the name of the Players struct to Player because it is too confusing. The compiler doesn't care, but humans do and it's too confusing.

struct Player
{
char name[SIZE]; //Player's Name
int playNum; //Player's Number
int Points; //Point's Scored
};
int TotalPoints(Player* players, int NUM_PLAYERS)
{
    // players is an array of type Player
    int total = 0;
    for (int index = 0; index < NUM_PLAYERS; index++)
    {
        Player player = players[index]; // player is a single element of type Player
        int pointsForThisPlayer = player.Points;
        total += pointsForThisPlayer;
    }

    return total;
}

It took me a long time, but i think i got the part of getting the total points working. The last part i have to make this thing do is find the person with the highest points scored and display his name and number. How can i do something like that.

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

const int SIZE = 50;
int TotalPoints(int *, int);

struct Players
{
       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.
    Players *players = new Players[NUM_PLAYERS];      //Array of structures
    int index;										//Loop
	int total=0;

    // Get Player data.
    cout << "\nYou will need the following information.\n";
	cout << "Pertaining to your Soccer Players.\n";
    cout << "The Player's Names, Player's Numbers\n";
	cout << "Finally you will need the Points Scored by Players.\n\n\n";
    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\n";
			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\n";
			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:\n\n";
	cout << "    Name    Number    Score	\n";
	cout << "--------------------------------\n";

	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 << "\n\nThe total of points scored by the team are: ";
	cout << total << endl;

	// Delete the memory.
	delete [] players;

    return 0;

}

Thanks vernon, i had posted what i did to fix that part. I didn't know you had posted a message too. But I appreciate you explaining me a bit more what i was doing wrong. Its greatly appreciated. I'm now trying to work on my last part of the puzzle.

To get a maximum, set up a max variable, initialize max to the first element, then loop through the other elements, check whether each element is greater than max. If it is, set max to equal that element. Here's some same code. You'll need to change it to the needs of your struct, but the algorithm for finding the max remains the same. You'll have the Player. Extract the relevant information.

#include <iostream>
using namespace std;

int main ()
{ 
    int array[5] = {6, 3, 2, 8, 5};    
    int max = array[0];
    int maxIndex = 0;
    for (int i = 0; i < 5; i++)
    {
      if (array[i] > max)
      {
          max = array[i];
          maxIndex = i;
      }
    } 
    
    cout << "highest score: " << max << endl;
    cout << "It is element " << maxIndex << " in the array" << endl;
 
    cin.get();
    return 0;
}

Thanks vernon, i had posted what i did to fix that part. I didn't know you had posted a message too. But I appreciate you explaining me a bit more what i was doing wrong. Its greatly appreciated. I'm now trying to work on my last part of the puzzle.

No problem. Just posted something else too. Hopefully that'll help you solve it.

commented: For the effort +24

Sorry about that, i meant to say player[index].points.

Ok, i have been working on the last advice given to me and this is what i have came up with so far. I apologyse ahead of time if i don't make much sence i haven't had much sleep, trying to figure this out. In the first part for int Player[12]= the values inside the array,
this part i keep on getting it wrong. And at the end i have an idea but not sure if it will work because i have to get the program to run first. But since you guys are the experts let me know if im compiling the code wrong and if is it a realistic idea the one i got. Thanks

//To get the player with most points
	int Player[12]; //This part I'm not sure how to link it to what the program 
		//has stored in the array its supposed to be Players[12] = ?
    int max = Player[0];
    int maxIndex = 0;
    for (int index = 0; index < 5; index++)
    {
		if (players[index].Points > max)
      {
		  max = players[index].Points;
          maxIndex = index;
      }
    } 
    
    cout << "highest score: " << max << endl;
    cout << "It is element " << maxIndex << " in the array" << endl;

	//After finding out in what place the max is could i use that same maxIndex to get 
	//his name and number like shown below or would that be completely wrong. 
	cout << "The soccer players name is: " << players[maxIndex].name << endl;
	cout << "The soccer players number is: " << players[maxIndex].playNum << endl;
 
    cin.get();
    
	// Delete the memory.
	delete [] players;

    return 0;

I have only added the last part of the section, and i did change the word Players to Player like vernon said so that it makes it easier to read. That is line 10, 22. From my post at 3 am It actually helped me some to because i started to get confused my self.

Might be best at this point to give you a full working example. You are still having some problems regarding the dereferencing. Hopefully this'll help.

#include <iostream>
#include <cstring>
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
};


int main ()
{
    char names[5][SIZE] = {"Bob", "Phil", "Sam", "Gary", "Ted"};
    Player* players = new Player[5];
    // initialize array
    strcpy (players[0].name, names[0]);
    players[0].playNum = 5;
    players[0].Points = 12;
    strcpy (players[1].name, names[1]);
    players[1].playNum = 13;
    players[1].Points = 20;
    strcpy (players[2].name, names[2]);
    players[2].playNum = 3;
    players[2].Points = 22;
    strcpy (players[3].name, names[3]);
    players[3].playNum = 2;
    players[3].Points = 10;
    strcpy (players[4].name, names[4]);
    players[4].playNum = 44;
    players[4].Points = 17;
    
    // calculate max
    int max = players[0].Points;
    int maxIndex = 0;
    for (int i = 1; i < 5; i++)
    {
      if (players[i].Points > max)
      {
          max = players[i].Points;
          maxIndex = i;
      }
    } 
    
    // display max
    cout << "highest score: " << players[maxIndex].name << " " << players[maxIndex].Points << endl;
    cout << "It is element " << maxIndex << " in the array" << endl;
 
    // pause
    cin.get();
    
    // Delete the memory.
    delete [] players; 
       
    return 0;
}

I read the advice, I re-read the book, and i took a nice break to get a bit distracted and relaxed and finally took everything and assembled the program in baby steps. It actually works. So my question to you guys is those it look ok.

#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 << "\nYou will need the following information.\n";
	cout << "Pertaining to your Soccer Players.\n";
    cout << "The Player's Names, Player's Numbers\n";
	cout << "Finally you will need the Points Scored by Players.\n\n\n";
    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\n";
			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\n";
			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:\n\n";
	cout << "    Name    Number    Score	\n";
	cout << "--------------------------------\n";

	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 << "\n\nThe 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;

}

Looks OK to me. Didn't run it. Does it work?

Yes it works, Once again thanks for seeing me through this Vernon.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.