fork download
  1. //Andrew Alspaugh CS1A Chapter 11. P.646. #6
  2. //
  3. /*****************************************************************************
  4. Soccer Scores
  5. ____________________________________________________________________________
  6. This program stores and manages data for a soccer team. It asks the user to
  7. enter each player's name, number, and points scored. The program displays a
  8. table listing all players with their numbers and points, calculates the total
  9. points scored by the team, and identifies the player with the most points.
  10. ____________________________________________________________________________
  11. //Data Dictionary
  12. //Inputs
  13.  
  14.   const int teamSize = 12;
  15.   Player team[teamSize]; // array to store 12 player structures
  16.  
  17. //Outputs
  18.  
  19.   int totalPoints = 0; // total points scored by the team
  20.   int maxPoints = -1; // highest points scored by a player
  21.   int topPlayerIndex = 0; // index of the top scoring player
  22. *****************************************************************************/
  23.  
  24. #include <iostream>
  25. #include <string>
  26. using namespace std;
  27.  
  28. struct Player
  29. {
  30. string name;
  31. int number;
  32. int points;
  33. };
  34.  
  35. int main()
  36. {
  37. //DATA DICTIONARY
  38. const int teamSize = 12;
  39. Player team[teamSize];
  40.  
  41. int totalPoints = 0;
  42. int maxPoints = -1;
  43. int topPlayerIndex = 0;
  44.  
  45. //INPUT
  46. // Input data for each player
  47. for(int i = 0; i < teamSize; i++)
  48. {
  49. cout << "Enter name of player " << i + 1 << " :" << endl;
  50. cin >> ws; // Consume any leftover newline characters
  51. getline(cin, team[i].name);
  52.  
  53. cout << "Enter number of player " << i + 1 << " :" << endl;
  54. cin >> team[i].number;
  55.  
  56. cout << "Enter points scored by player " << i + 1 << " :" << endl;
  57. cin >> team[i].points;
  58.  
  59. totalPoints += team[i].points;
  60.  
  61. if(team[i].points > maxPoints)
  62. {
  63. maxPoints = team[i].points;
  64. topPlayerIndex = i;
  65. }
  66. }
  67.  
  68. //OUTPUT
  69. // Display table
  70. cout << endl;
  71. cout << "Player Number\tName\t\tPoints" << endl;
  72. cout << "----------------------------------------" << endl;
  73. for(int i = 0; i < teamSize; i++)
  74. {
  75. cout << team[i].number << "\t\t" << team[i].name << "\t\t" << team[i].points << endl;
  76. }
  77.  
  78. // Display totals and top scorer
  79. cout << endl;
  80. cout << "Total points by the team: " << totalPoints << endl;
  81. cout << "Top scorer: " << team[topPlayerIndex].name
  82. << " (Number: " << team[topPlayerIndex].number
  83. << ", Points: " << team[topPlayerIndex].points << ")" << endl;
  84.  
  85. return 0;
  86. }
Success #stdin #stdout 0s 5316KB
stdin
Alice
10
5
Bob
7
8
Charlie
9
12
Diana
11
7
Ethan
8
10
Fiona
6
4
George
12
15
Hannah
5
9
Ian
4
3
Julia
3
6
Kevin
2
11
Laura
1
14
stdout
Enter name of player 1 :
Enter number of player 1 :
Enter points scored by player 1 :
Enter name of player 2 :
Enter number of player 2 :
Enter points scored by player 2 :
Enter name of player 3 :
Enter number of player 3 :
Enter points scored by player 3 :
Enter name of player 4 :
Enter number of player 4 :
Enter points scored by player 4 :
Enter name of player 5 :
Enter number of player 5 :
Enter points scored by player 5 :
Enter name of player 6 :
Enter number of player 6 :
Enter points scored by player 6 :
Enter name of player 7 :
Enter number of player 7 :
Enter points scored by player 7 :
Enter name of player 8 :
Enter number of player 8 :
Enter points scored by player 8 :
Enter name of player 9 :
Enter number of player 9 :
Enter points scored by player 9 :
Enter name of player 10 :
Enter number of player 10 :
Enter points scored by player 10 :
Enter name of player 11 :
Enter number of player 11 :
Enter points scored by player 11 :
Enter name of player 12 :
Enter number of player 12 :
Enter points scored by player 12 :

Player Number	Name		Points
----------------------------------------
10		Alice		5
7		Bob		8
9		Charlie		12
11		Diana		7
8		Ethan		10
6		Fiona		4
12		George		15
5		Hannah		9
4		Ian		3
3		Julia		6
2		Kevin		11
1		Laura		14

Total points by the team: 104
Top scorer: George (Number: 12, Points: 15)