fork download
  1. // Wyatt Carey CS1A Practicum Practice
  2. //******************************************************************************
  3. // Baseball Player Statistics
  4. //******************************************************************************
  5. //
  6. //
  7. //
  8. //
  9. #include <iostream>
  10. #include <string>
  11. #include <iomanip>
  12. using namespace std;
  13.  
  14. // Create strcuture.
  15. struct PlayerInfo
  16. {
  17. string name;
  18. int homeRuns;
  19. int hits;
  20. };
  21.  
  22. // Function prototypes.
  23. void showInfo(PlayerInfo [], const int);
  24. void showMenu ();
  25. int searchPlayers(PlayerInfo [], string, const int);
  26. void changeInfo(PlayerInfo stats [], int pos);
  27.  
  28. int main()
  29. {
  30. const int NUM_PLAYERS = 10;
  31. PlayerInfo stats[NUM_PLAYERS] = {{"Mike T", 2, 5},
  32. {"Josh G", 4, 5},
  33. {"Luke A", 5, 6},
  34. {"Ian M", 4, 4},
  35. {"Jared G", 3, 5},
  36. {"Blake A", 12, 45},
  37. {"Liam F", 23, 43},
  38. {"Adam A", 22, 34},
  39. {"Kian D", 32, 43},
  40. {"Yoiurel A", 23, 34}};
  41. int choice; // Input : Holds menu choice.
  42. string search; // Input : Name of players to search within array.
  43. int pos; // Output : Position of player found in the array,
  44. // set to -1 if player is not found or user
  45. // quit.
  46. // Display initial array.
  47. showInfo(stats, NUM_PLAYERS);
  48.  
  49. // Display menu.
  50. showMenu();
  51.  
  52. do
  53. {
  54. // Input : Get Menu Choice.
  55. cin >> choice;
  56. cout << endl << endl;
  57. cin.ignore();
  58. if (choice = 1)
  59. {
  60. cout << "Enter the player's name: \n";
  61. getline(cin, search);
  62. cout << endl;
  63. searchPlayers(stats, search, NUM_PLAYERS);
  64. }
  65.  
  66. cout << endl;
  67.  
  68. // Output : Display Updated Data.
  69. if (pos != -1) // Won't display data if player doesn't exist
  70. {
  71. changeInfo(stats, pos);
  72. cout << "Update player info: \n\n";
  73. showInfo(stats, NUM_PLAYERS);
  74. }
  75.  
  76. // Input : Prompt the choice if the user did not already quit.
  77. if (choice != 0)
  78. {
  79. showMenu();
  80. cin >> choice;
  81. }
  82. } while(choice != 0);
  83. cout << "\n\nExiting program...";
  84. return 0;
  85. }
  86.  
  87. // This function displays the info sheet of the players.
  88. void showInfo (PlayerInfo stats[], const int NUM_PLAYERS)
  89. {
  90. int index;
  91.  
  92. cout << "Player Stats" << endl;
  93. cout << "____________________________________________" << endl;
  94. cout << left << setw(8) << "Name" << setw(18) << "Home Runs" << setw(18)
  95. << "Hits" << endl;
  96. for (index = 0; index < NUM_PLAYERS; index++)
  97. {
  98. cout << setw(18) << stats[index].name
  99. << setw(18) << stats[index].homeRuns
  100. << setw(18) << stats[index].hits << endl;
  101. }
  102. cout << endl;
  103.  
  104. }
  105.  
  106. // This simple function shows a menu of choices to adjust a player's data or
  107. // quit.
  108. void showMenu ()
  109. {
  110. cout << "What option are you choosing?\n";
  111. cout << "1. Change Player Data" << endl;
  112. cout << "0. Quit" << endl << endl << "Enter your choice: \n";
  113.  
  114. }
  115.  
  116. // This function uses an input name to search for a player
  117. int searchPlayers(PlayerInfo stats [], string search, const int NUM_PLAYERS)
  118. {
  119. int index; // Index for the loop.
  120. int pos; // Position of the player array for given player's name.
  121. bool found; // flag indicating the correct player was found.
  122.  
  123. do
  124. {
  125. for (index = 0, pos = -1, found = false; index < NUM_PLAYERS && !found;
  126. index++)
  127. {
  128. /* If the player's name is found within the strings in the array, a
  129. value is returned indicating where. If it is not, it returns a
  130. very large number. As long as the number is less than 100, that
  131. value indicates a legitimate position was found*/
  132. if(stats[index].name.find(search, 0) < 100)
  133. {
  134. found = true;
  135. pos = index;
  136. }
  137. }
  138. if (pos == -1)
  139. {
  140. cout << "Sorry, that player doesn't exist.\n"
  141. << "Enter another name to try again, or enter 0 to quit: \n";
  142. getline(cin, search);
  143. cout << endl;
  144. }
  145. } while(search != "0" && found == false);
  146. return pos;
  147. }
  148.  
  149. // This function adjusts the data stored for each player.
  150. void changeInfo (PlayerInfo stats [], int pos)
  151. {
  152. int choice; // Input : Determines which data to change.
  153.  
  154. cout << "Which stats you wish to updata?" << endl;
  155. cout << "1. Home Runs" << endl;
  156. cout << "2. Hits\n\n";
  157. cout << "Enter your choice: \n";
  158. cin >> choice;
  159. cout << endl << endl;
  160. switch(choice)
  161. {
  162. case 1:
  163. cout << "Enter the new number of home runs: \n";
  164. cin >> stats[pos].homeRuns;
  165. cout << endl << endl;
  166. break;
  167. case 2:
  168. cout << "Enter the new number of hits: \n";
  169. cin >> stats[pos].hits;
  170. cout << endl << endl;
  171. break;
  172. }
  173. }
Success #stdin #stdout 0s 5300KB
stdin
1
Mike T
2
17
0
stdout
Player Stats
____________________________________________
Name    Home Runs         Hits              
Mike T            2                 5                 
Josh G            4                 5                 
Luke A            5                 6                 
Ian M             4                 4                 
Jared G           3                 5                 
Blake A           12                45                
Liam F            23                43                
Adam A            22                34                
Kian D            32                43                
Yoiurel A         23                34                

What option are you choosing?
1. Change Player Data
0. Quit

Enter your choice: 


Enter the player's name: 


Which stats you wish to updata?
1. Home Runs
2. Hits

Enter your choice: 


Enter the new number of hits: 


Update player info: 

Player Stats
____________________________________________
Name    Home Runs         Hits              
Mike T            2                 17                
Josh G            4                 5                 
Luke A            5                 6                 
Ian M             4                 4                 
Jared G           3                 5                 
Blake A           12                45                
Liam F            23                43                
Adam A            22                34                
Kian D            32                43                
Yoiurel A         23                34                

What option are you choosing?
1. Change Player Data
0. Quit

Enter your choice: 


Exiting program...