/* Sam Trivikraman CS1A Practice Practicum
*
________________________________________________________________________________
Store Baseball Player Data
________________________________________________________________________________
This program stores user inputted data pertaining to baseball players. The
user is also allowed to change the written data after.
________________________________________________________________________________
INPUTS
player name //The baseball player's name
home runs //The number of home runs a player has hit
hits //The total number of hits the player has had
menu choice //Which menu selection the user chose (to change hits)
or home runs
OUTPUTS
baseball player data list //An array which contains the player's name, number
of hits, and number of home runs
________________________________________________________________________________
*
*/
#include <iostream>
#include <string>
using namespace std;
//The structure which contains the baseball player data
struct baseballData
{
string playerName; //INPUT the baseball player's name
int homeRuns; //INPUT the number of home runs
int hits; //INPUT the number of hits
};
//Function which takes in the inputs and puts them into an array
void input(baseballData players[], int size)
{
for(int i = 0; i < size; i++)
{
cout << "Please enter the player's name." << endl;
cin >> players[i].playerName;
cout << "Please enter the number of home runs this player had." << endl;
cin >> players[i].homeRuns;
cout << "Please enter the number of hits this player had." << endl;
cin >> players[i].hits;
}
}
//Function which outputs the contents of the array
void output(baseballData players[], int size)
{
cout << "\n Player Data \n" << endl;
for(int i = 0; i < size; i++)
{
cout << "Name: " << players[i].playerName << endl;
cout << "Home Runs: " << players[i].homeRuns << endl;
cout << "Hits: " << players[i].hits << endl;
}
}
//A linear search which sorts through the array, and finds the index of the player
//The function replaces the specified information with new user inputted data for this player
void linearSearch(baseballData players[], int size)
{
int menuChoice; //INPUT which menu selection the user chose
string playerName; //The player name the function has to search for (to find the index of the player)
cout << "Please enter the player's name that you are looking for." << endl;
cin >> playerName;
//Linear search to look through the array and replace the necessary information
for(int i = 0; i < 10; i++)
{
if(playerName == players[i].playerName)
{
do
{
cout << "Please enter the corresponding number for what you would like to change, or type 0 to end the program." << endl;
cout << "1. Number of Home Runs." << endl;
cout << "2. Number of total Hits." << endl;
cin >> menuChoice;
if(menuChoice == 1)
{
cout << "Please input the new number of home runs for this player." << endl;
cin >> players[i].homeRuns;
cout << "The new number of home runs for " << playerName << " is " << players[i].homeRuns << endl;
}
else if(menuChoice == 2)
{
cout << "Please input the new number of total hits for this player." << endl;
cin >> players[i].hits;
cout << "The new number of hits for " << playerName << " is " << players[i].hits << endl;
}
} while(menuChoice != 0);
}
}
}
int main() {
baseballData players[10]; //OUTPUT the array of baseball player data
//call input function
input(players, 10);
//call output function
output(players, 10);
//call linear search function
linearSearch(players, 10);
cout << "New Updated data for the baseball players." << endl;
//call output function for the updated array
output(players, 10);
return 0;
}