fork download
  1. /* Sam Trivikraman CS1A Practice Practicum
  2. *
  3. ________________________________________________________________________________
  4. Store Baseball Player Data
  5. ________________________________________________________________________________
  6. This program stores user inputted data pertaining to baseball players. The
  7. user is also allowed to change the written data after.
  8. ________________________________________________________________________________
  9. INPUTS
  10.  
  11. player name //The baseball player's name
  12. home runs //The number of home runs a player has hit
  13. hits //The total number of hits the player has had
  14. menu choice //Which menu selection the user chose (to change hits)
  15. or home runs
  16.  
  17. OUTPUTS
  18.  
  19. baseball player data list //An array which contains the player's name, number
  20. of hits, and number of home runs
  21. ________________________________________________________________________________
  22. *
  23. */
  24.  
  25. #include <iostream>
  26. #include <string>
  27. using namespace std;
  28.  
  29. //The structure which contains the baseball player data
  30. struct baseballData
  31. {
  32.  
  33. string playerName; //INPUT the baseball player's name
  34. int homeRuns; //INPUT the number of home runs
  35. int hits; //INPUT the number of hits
  36. };
  37.  
  38. //Function which takes in the inputs and puts them into an array
  39. void input(baseballData players[], int size)
  40. {
  41. for(int i = 0; i < size; i++)
  42. {
  43. cout << "Please enter the player's name." << endl;
  44. cin >> players[i].playerName;
  45. cout << "Please enter the number of home runs this player had." << endl;
  46. cin >> players[i].homeRuns;
  47. cout << "Please enter the number of hits this player had." << endl;
  48. cin >> players[i].hits;
  49.  
  50. }
  51. }
  52.  
  53. //Function which outputs the contents of the array
  54. void output(baseballData players[], int size)
  55. {
  56. cout << "\n Player Data \n" << endl;
  57. for(int i = 0; i < size; i++)
  58. {
  59. cout << "Name: " << players[i].playerName << endl;
  60. cout << "Home Runs: " << players[i].homeRuns << endl;
  61. cout << "Hits: " << players[i].hits << endl;
  62. }
  63. }
  64.  
  65. //A linear search which sorts through the array, and finds the index of the player
  66. //The function replaces the specified information with new user inputted data for this player
  67. void linearSearch(baseballData players[], int size)
  68. {
  69. int menuChoice; //INPUT which menu selection the user chose
  70. string playerName; //The player name the function has to search for (to find the index of the player)
  71.  
  72.  
  73. cout << "Please enter the player's name that you are looking for." << endl;
  74. cin >> playerName;
  75.  
  76. //Linear search to look through the array and replace the necessary information
  77. for(int i = 0; i < 10; i++)
  78. {
  79.  
  80. if(playerName == players[i].playerName)
  81. {
  82. do
  83. {
  84. cout << "Please enter the corresponding number for what you would like to change, or type 0 to end the program." << endl;
  85. cout << "1. Number of Home Runs." << endl;
  86. cout << "2. Number of total Hits." << endl;
  87. cin >> menuChoice;
  88.  
  89. if(menuChoice == 1)
  90. {
  91. cout << "Please input the new number of home runs for this player." << endl;
  92. cin >> players[i].homeRuns;
  93. cout << "The new number of home runs for " << playerName << " is " << players[i].homeRuns << endl;
  94. }
  95. else if(menuChoice == 2)
  96. {
  97. cout << "Please input the new number of total hits for this player." << endl;
  98. cin >> players[i].hits;
  99. cout << "The new number of hits for " << playerName << " is " << players[i].hits << endl;
  100. }
  101.  
  102. } while(menuChoice != 0);
  103. }
  104. }
  105. }
  106.  
  107. int main() {
  108.  
  109. baseballData players[10]; //OUTPUT the array of baseball player data
  110.  
  111. //call input function
  112. input(players, 10);
  113. //call output function
  114. output(players, 10);
  115. //call linear search function
  116. linearSearch(players, 10);
  117.  
  118. cout << "New Updated data for the baseball players." << endl;
  119. //call output function for the updated array
  120. output(players, 10);
  121.  
  122.  
  123. return 0;
  124. }
Success #stdin #stdout 0.01s 5268KB
stdin
Bob
4
5
Joe
2
4
Jeff
2
4
Sally
5
7
Moe
7
8
Sam
10
2
Ben
4
5
Jackson
5
6
Anya
8
9
Thalia
2
1
Joe
2
9
0
stdout
Please enter the player's name.
Please enter the number of home runs this player had.
Please enter the number of hits this player had.
Please enter the player's name.
Please enter the number of home runs this player had.
Please enter the number of hits this player had.
Please enter the player's name.
Please enter the number of home runs this player had.
Please enter the number of hits this player had.
Please enter the player's name.
Please enter the number of home runs this player had.
Please enter the number of hits this player had.
Please enter the player's name.
Please enter the number of home runs this player had.
Please enter the number of hits this player had.
Please enter the player's name.
Please enter the number of home runs this player had.
Please enter the number of hits this player had.
Please enter the player's name.
Please enter the number of home runs this player had.
Please enter the number of hits this player had.
Please enter the player's name.
Please enter the number of home runs this player had.
Please enter the number of hits this player had.
Please enter the player's name.
Please enter the number of home runs this player had.
Please enter the number of hits this player had.
Please enter the player's name.
Please enter the number of home runs this player had.
Please enter the number of hits this player had.

		Player Data		

Name: Bob
Home Runs: 4
Hits: 5
Name: Joe
Home Runs: 2
Hits: 4
Name: Jeff
Home Runs: 2
Hits: 4
Name: Sally
Home Runs: 5
Hits: 7
Name: Moe
Home Runs: 7
Hits: 8
Name: Sam
Home Runs: 10
Hits: 2
Name: Ben
Home Runs: 4
Hits: 5
Name: Jackson
Home Runs: 5
Hits: 6
Name: Anya
Home Runs: 8
Hits: 9
Name: Thalia
Home Runs: 2
Hits: 1
Please enter the player's name that you are looking for.
Please enter the corresponding number for what you would like to change, or type 0 to end the program.
1. Number of Home Runs.
2. Number of total Hits.
Please input the new number of total hits for this player.
The new number of hits for Joe is 9
Please enter the corresponding number for what you would like to change, or type 0 to end the program.
1. Number of Home Runs.
2. Number of total Hits.
New Updated data for the baseball players.

		Player Data		

Name: Bob
Home Runs: 4
Hits: 5
Name: Joe
Home Runs: 2
Hits: 9
Name: Jeff
Home Runs: 2
Hits: 4
Name: Sally
Home Runs: 5
Hits: 7
Name: Moe
Home Runs: 7
Hits: 8
Name: Sam
Home Runs: 10
Hits: 2
Name: Ben
Home Runs: 4
Hits: 5
Name: Jackson
Home Runs: 5
Hits: 6
Name: Anya
Home Runs: 8
Hits: 9
Name: Thalia
Home Runs: 2
Hits: 1