944,010 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2328
  • C++ RSS
Nov 28th, 2006
0

Help with storing data in structure

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6. const int SIZE = 25;
  7.  
  8. struct Players
  9. {
  10. char name[SIZE]; //Player's Name
  11. int playNum; //Player's Number
  12. double Points; //Point's Scored
  13. };
  14.  
  15. int main()
  16. {
  17. const int NUM_PLAYERS = 5; //Number of Players
  18. Players players[NUM_PLAYERS]; //Array of sturctures
  19. int index; //Loop
  20.  
  21. // Get Player data.
  22. cout << "Enter the players by ";
  23. cout << " players, numbers, and their scores.\n";
  24. for (index = 0; index < NUM_PLAYERS; index++)
  25. {
  26. cout << "Please enter players name: ";
  27. cin.ignore();
  28. cin.getline(players.name[SIZE];
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
joelw is offline Offline
28 posts
since Nov 2006
Nov 28th, 2006
0

Re: Help with storing data in structure

Click to Expand / Collapse  Quote originally posted by joelw ...
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)
  1. cin.getline(players.name[SIZE];
C++ Syntax (Toggle Plain Text)
  1. cin.getline(players.name[index];
would work better
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006
Nov 28th, 2006
0

Re: Help with storing data in structure

>> 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);
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Nov 28th, 2006
0

Re: Help with storing data in structure

This should realy work..

C++ Syntax (Toggle Plain Text)
  1. //cin.ignore(); // Will skip first character in input so remove
  2. 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.
Reputation Points: 10
Solved Threads: 1
Light Poster
disc is offline Offline
29 posts
since Sep 2006
Nov 29th, 2006
0

Re: Help with storing data in structure

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)
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. const int SIZE = 25;
  8.  
  9. struct Players
  10. {
  11. char name[SIZE]; //Player's Name
  12. int playNum; //Player's Number
  13. double Points; //Point's Scored
  14. };
  15.  
  16. int main()
  17. {
  18. const int NUM_PLAYERS = 5; //Number of Players
  19. // Dynamically allocate the memory needed.
  20. Players *players = new Players[NUM_PLAYERS]; //Array of sturctures
  21. int index; //Loop
  22.  
  23. // Get Player data.
  24. cout << "Enter the players by ";
  25. cout << " players, numbers, and their scores.\n";
  26. for (index = 0; index < NUM_PLAYERS; index++)
  27. {
  28. cout << "Please enter players name: ";
  29. cin.ignore();
  30. cin.getline( players[index].name, 25 );
  31. cout << "Please enter players number: ";
  32. ( cin >> players[index].playNum ).get();
  33. cout << "Please enter points scored by player: ";
  34. ( cin >> players[index].Points ).get();
  35.  
  36. }
  37.  
  38. //Display the players data
  39. cout << "Here is the players data:\n";
  40. for (index = 0; index < NUM_PLAYERS; index++)
  41. {
  42. cout << "Name: " << players->name << endl;
  43. cout << "Number: " << players->playNum << endl;
  44. cout << "Score: " << players->Points << endl;
  45. }
  46. // Delete the memory.
  47. delete [] players;
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54. return 0;
  55.  
  56.  
  57.  
  58. }
Last edited by joelw; Nov 29th, 2006 at 8:07 am.
Reputation Points: 10
Solved Threads: 0
Light Poster
joelw is offline Offline
28 posts
since Nov 2006
Nov 29th, 2006
0

Re: Help with storing data in structure

You need to use the index when displaying the players data. Now only the first player is displayed.
Reputation Points: 10
Solved Threads: 1
Light Poster
disc is offline Offline
29 posts
since Sep 2006
Nov 29th, 2006
0

Re: Help with storing data in structure

Yep, u need to use the index in your loop to make any sense of using it.. something like
C++ Syntax (Toggle Plain Text)
  1. for (index=0; index<NUM_PLAYERS; index++)
  2. {
  3. cout << "Name: " << players[index].name << endl;
  4. cout << "Number: " << players[index].playNum << endl;
  5. cout << "Score: " << players[index].Points << endl;
  6. }
This way, each player's details are outputted starting from index 0 to the NUM_PLAYERS-1
Reputation Points: 13
Solved Threads: 2
Junior Poster in Training
may4life is offline Offline
57 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: I'm lost and need some help
Next Thread in C++ Forum Timeline: Rational Number Constructor





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC