fork download
  1. //Alessandro Iniguez CSC5 Chapter 11, P. 646, #6
  2. //
  3. /******************************************************************************
  4.  *
  5.  * SOCCER PLAYER
  6.  * _____________________________________________________________________________
  7.  * The program should keep each element is for a different player on a team.
  8.  * When the program runs it should ask the user to enter the data for each
  9.  * player. It should then show a table that lists each players number, name,
  10.  * and points scored. The program should also calculate and display the total
  11.  * points earned by the team. The number and name of the player who has earned
  12.  * the most points should also be displayed
  13.  * _____________________________________________________________________________
  14.  * INPUT
  15.  * playNum : players number
  16.  *
  17.  * OUTPUT
  18.  * Points : Scored goals
  19.  * Name : Players name
  20.  *
  21.  ******************************************************************************/
  22. #include <iostream>
  23. #include <iomanip>
  24. #include <string>
  25. using namespace std;
  26.  
  27.  
  28. const int SIZE = 50;
  29. struct Player
  30. {
  31. char name[SIZE]; //Player's Name
  32. int playNum; //Player's Number
  33. int Points; //Point's Scored
  34. };
  35.  
  36. const int NUM_PLAYERS = 3; //Number of Players
  37. // Dynamically allocate the memory needed.
  38. Player *players = new Player[NUM_PLAYERS]; //Array of structures
  39. int total = 0;
  40.  
  41. void getPlayerInfo(Player &);
  42. void showInfo(Player);
  43. int getTotalPoints(Player[], int);
  44. void showHighest(Player[], int);
  45.  
  46. int main()
  47. {
  48. getPlayerInfo(*players);
  49. getTotalPoints(players,total);
  50. showInfo(*players);
  51.  
  52. }
  53.  
  54. void getPlayerInfo(Player&)
  55. {
  56. int index;
  57.  
  58.  
  59. // Get Player data.
  60. cout << "\nYou will need the following information.\n";
  61. cout << "Pertaining to your Soccer Players.\n";
  62. cout << "The Player's Names, Player's Numbers\n";
  63. cout << "Finally you will need the Points Scored by Players.\n\n\n";
  64. for (index = 0; index < NUM_PLAYERS; index++)
  65. {
  66. cout << "Please enter the Player's Name: ";
  67. cin.getline(players[index].name, 50);
  68. cout << "Please enter the Player's Number: ";
  69. (cin >> players[index].playNum).get();
  70.  
  71. //To test my values for zero, negative
  72. while (players[index].playNum <= 0)
  73. {
  74. cout << "Zero or negative numbers not allowed\n";
  75. cout << "Please enter the Player's Number: ";
  76. (cin >> players[index].playNum).get();
  77.  
  78. }
  79. cout << "Please enter the Points Scored by the Player: ";
  80. (cin >> players[index].Points).get();
  81.  
  82. //To test my values for zero, negative.
  83. while (players[index].Points < 0)
  84. {
  85. cout << "Zero or negative numbers not allowed\n";
  86. cout << "Please enter the Points Scored by the Player: ";
  87. (cin >> players[index].Points).get();
  88. }
  89. cout << endl << endl;
  90. }
  91. return;
  92. }
  93. int getTotalPoints(Player[], int)
  94. {
  95. int index;
  96. int total = 0;
  97. //Calculate the total points
  98. for (index = 0; index < NUM_PLAYERS; index++)
  99. {
  100. total += players[index].Points;
  101. }
  102. int TotalPoints(int *, int);
  103.  
  104. return total;
  105. }
  106. void showInfo(Player)
  107. {
  108. int TotalPoints = 0;
  109. int index = 0;
  110. //Display the players data
  111. cout << "Here is the players data:\n\n";
  112. cout << " Name Number Score \n";
  113. cout << "--------------------------------\n";
  114.  
  115. for (index = 0; index < NUM_PLAYERS; index++)
  116. {
  117. cout << setw(8) << players[index].name;
  118. cout << setw(8) << players[index].playNum;
  119. cout << setw(8) << players[index].Points << endl;
  120. }
  121. //Display the results of the total points.
  122. cout << "\n\nThe total of points scored by the team are: ";
  123. cout << TotalPoints << endl;
  124.  
  125. //To get the player with most points
  126.  
  127. int max = players[0].Points;
  128. int maxIndex = 0;
  129. for (int index = 0; index < 12; index++)
  130. {
  131. if (players[index].Points > max)
  132. {
  133. max = players[index].Points;
  134. maxIndex = index;
  135. }
  136.  
  137. }
  138. cout << "highest score by: " << players[maxIndex].name << " number: ";
  139. cout << players[maxIndex].playNum << endl;
  140. return;
  141. }
Success #stdin #stdout 0s 5284KB
stdin
messi
10
99
ronaldo
7
80
mhappe
10
120
stdout
You will need the following information.
Pertaining to your Soccer Players.
The Player's Names, Player's Numbers
Finally you will need the Points Scored by Players.


Please enter the Player's Name: Please enter the Player's Number: Please enter the Points Scored by the Player: 

Please enter the Player's Name: Please enter the Player's Number: Please enter the Points Scored by the Player: 

Please enter the Player's Name: Please enter the Player's Number: Please enter the Points Scored by the Player: 

Here is the players data:

    Name    Number    Score	
--------------------------------
   messi      10      99
 ronaldo       7      80
  mhappe      10     120


The total of points scored by the team are: 0
highest score by:  Score	
--------------------------------
   messi      10      99
Finally you will need the Points Scored by Players.


Please enter the Player's Name:  number: 1701146144