| | |
Help with storing data in structure
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2006
Posts: 28
Reputation:
Solved Threads: 0
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.
so far i have written this but some how i think im doing it wrong if anyone could help thanks in advance.
C++ Syntax (Toggle Plain Text)
#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.
C++ Syntax (Toggle Plain Text)
cin.getline(players.name[SIZE];
C++ Syntax (Toggle Plain Text)
cin.getline(players.name[index];
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
>> 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[SIZE];
that should be like this:
cin.getline(players[i].name, SIZE);
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[SIZE];
that should be like this:
cin.getline(players[i].name, SIZE);
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Sep 2006
Posts: 22
Reputation:
Solved Threads: 1
This should realy work..
And Ancient Dragon is right. An int should do for Points.
Tip: Put your const en struct in a headerfile.
C++ Syntax (Toggle Plain Text)
//cin.ignore(); // Will skip first character in input so remove cin.getline(players[index].name , SIZE);
Tip: Put your const en struct in a headerfile.
•
•
Join Date: Nov 2006
Posts: 28
Reputation:
Solved Threads: 0
ok my program is written now i need to calculate all the scores together and display the results here is my program so far.
C++ Syntax (Toggle Plain Text)
#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; }
Last edited by joelw; Nov 29th, 2006 at 8:07 am.
•
•
Join Date: Oct 2006
Posts: 57
Reputation:
Solved Threads: 2
Yep, u need to use the index in your loop to make any sense of using it.. something like
This way, each player's details are outputted starting from index 0 to the NUM_PLAYERS-1
C++ Syntax (Toggle Plain Text)
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; }
![]() |
Similar Threads
- Data Structure question (C++)
- a question about heap data structure (C)
- Data Structure Using JAVA (Java)
- How to delete a data structure in Builder 6.0? (C++)
- Discrete Math or Data structure (Computer Science)
Other Threads in the C++ Forum
- Previous Thread: I'm lost and need some help
- Next Thread: Rational Number Constructor
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline graph homeworkhelper iamthwee ifstream input int integer java lib linux list loop looping loops map math matrix memory multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






