ok, i need help writing a program that stores data about a basketball player in a structue. I need to create a structure for players name, players number, and by points the player scored. I need to keep an array of 5 of these structures. Which each element is for a different player on a team. The program should ask the user to enter data for each player. It should then show a table of each players number, name, and points scored and finally it should calculate and display the total points earned by the team.

so far i have written this but some how i think im doing it wrong if anyone could help thanks in advance.

#include <iostream>
#include <iomanip>

using namespace std;

const int SIZE = 25;

struct Players
{
       char name[SIZE];        //Player's Name
       int playNum;            //Player's Number
       double Points;          //Point's Scored
};    

int main()
{
    const int NUM_PLAYERS = 5; //Number of Players
    Players players[NUM_PLAYERS];      //Array of sturctures
    int index;                 //Loop
    
    // Get Player data.
    cout << "Enter the players by ";
    cout << " players, numbers, and their scores.\n";
    for (index = 0; index < NUM_PLAYERS; index++)
    {
        cout << "Please enter players name: ";
        cin.ignore(); 
        cin.getline(players.name[SIZE];

so far i have written this but some how i think im doing it wrong if anyone could help thanks in advance.

cin.getline(players.name[SIZE];
cin.getline(players.name[index];

would work better ;)

>> double Points; //Point's Scored
why are you using double here? Can value of Points have a fractional part (I doubt it)? I think you would be better off using either int or long.


>> cin.getline(players.name;
that should be like this:
cin.getline(players.name, SIZE);

This should realy work..

//cin.ignore();    // Will skip first character in input so remove
cin.getline(players[index].name , SIZE);

And Ancient Dragon is right. An int should do for Points.

Tip: Put your const en struct in a headerfile.

ok my program is written now i need to calculate all the scores together and display the results here is my program so far.

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

using namespace std;

const int SIZE = 25;

struct Players
{
       char name[SIZE];        //Player's Name
       int playNum;            //Player's Number
       double Points;          //Point's Scored
};

int main()
{
    const int NUM_PLAYERS = 5; //Number of Players
	// Dynamically allocate the memory needed.
    Players *players = new Players[NUM_PLAYERS];      //Array of sturctures
    int index;                 //Loop

    // Get Player data.
    cout << "Enter the players by ";
    cout << " players, numbers, and their scores.\n";
    for (index = 0; index < NUM_PLAYERS; index++)
    {
        cout << "Please enter players name: ";
        cin.ignore();
		cin.getline( players[index].name, 25 );
        cout << "Please enter players number: ";
        ( cin >> players[index].playNum ).get();
        cout << "Please enter points scored by player: ";
        ( cin >> players[index].Points ).get();

    } 

	//Display the players data
	cout << "Here is the players data:\n";
	for (index = 0; index < NUM_PLAYERS; index++)
	{
		cout << "Name: " << players->name << endl;
		cout << "Number: " << players->playNum << endl;
		cout << "Score: " << players->Points << endl;
	}
	// Delete the memory.
	delete [] players;






    return 0;



}

You need to use the index when displaying the players data. Now only the first player is displayed.

Yep, u need to use the index in your loop to make any sense of using it.. something like

for (index=0; index<NUM_PLAYERS; index++)
    {
        cout << "Name: "   << players[index].name    << endl;
        cout << "Number: " << players[index].playNum << endl;
        cout << "Score: "  << players[index].Points  << endl;
    }

This way, each player's details are outputted starting from index 0 to the NUM_PLAYERS-1

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