Help with storing data in structure

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2006
Posts: 28
Reputation: joelw is an unknown quantity at this point 
Solved Threads: 0
joelw joelw is offline Offline
Light Poster

Help with storing data in structure

 
0
  #1
Nov 28th, 2006
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.

  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];
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Help with storing data in structure

 
0
  #2
Nov 28th, 2006
Originally Posted by joelw View Post
so far i have written this but some how i think im doing it wrong if anyone could help thanks in advance.

  1. cin.getline(players.name[SIZE];
  1. cin.getline(players.name[index];
would work better
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,473
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Help with storing data in structure

 
0
  #3
Nov 28th, 2006
>> 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);
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 22
Reputation: disc is an unknown quantity at this point 
Solved Threads: 1
disc disc is offline Offline
Newbie Poster

Re: Help with storing data in structure

 
0
  #4
Nov 28th, 2006
This should realy work..

  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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 28
Reputation: joelw is an unknown quantity at this point 
Solved Threads: 0
joelw joelw is offline Offline
Light Poster

Re: Help with storing data in structure

 
0
  #5
Nov 29th, 2006
ok my program is written now i need to calculate all the scores together and display the results here is my program so far.


  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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 22
Reputation: disc is an unknown quantity at this point 
Solved Threads: 1
disc disc is offline Offline
Newbie Poster

Re: Help with storing data in structure

 
0
  #6
Nov 29th, 2006
You need to use the index when displaying the players data. Now only the first player is displayed.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 57
Reputation: may4life is an unknown quantity at this point 
Solved Threads: 2
may4life may4life is offline Offline
Junior Poster in Training

Re: Help with storing data in structure

 
0
  #7
Nov 29th, 2006
Yep, u need to use the index in your loop to make any sense of using it.. something like
  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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC