c++ A menu-driven program gives the user the option to find statistics about dif
ID: 3750985 • Letter: C
Question
c++
A menu-driven program gives the user the option to find statistics about different baseball players. The program reads from a file and stores the data for ten baseball players, including player’s team, name of player, number of homeruns, batting average, and runs batted in. (You can make up your data, or get it online, for example: http://espn.go.com/mlb/statistics )
Write a program that declares a struct to store the data for a player. Declare an array of 10 components to store the data for 10 baseball players.
The program prints out a menu (in a loop, so this can be done again and again) giving the user a choice to:
print out all users and statistics
print out the statistics for a specific player
print out all data for a specific team
update the data for a particular player (change one of the statistics)
DO ALL WORK IN FUNCTIONS. USE A FUNCTION TO READ THE DATA, A FUNCTION TO PRINT THE MENU, and FUNCTIONS FOR EACH OF THE MENU OPTIONS.
Before the program terminates, give the user the option to store the data in an output file.
Explanation / Answer
void setPlayerData(baseBallPlayerType playerList[], int listSize);
void getPlayerData(baseBallPlayerType playerList[], int listSize);
void getDataFromFile(ifstream& infile, baseBallPlayerType playerList[], int listSize);
void saveData(ofstream& outData, baseBallPlayerType playerList[], int listSize);
int searchPlayer (baseBallPlayerType playerList[], int listSize, string name);
void update(baseBallPlayerType playerList[], int listSize, int index);
int main ()
{
ifstream infile;
ofstream outfile;
int choice, index;
string name;
baseBallPlayerType playerList [NO_OF_BASEBALL_PLAYER];
do
{
cout << " 1. Enter Player's Data through prompt" << endl;
cout << " 2. Show Player's Data provided by user" << endl;
cout << " 3. Load Player's info Stored in File to Player's List" << endl;
cout << " 4. search for the Position of a Player" << endl;
cout << " 5. Update Player information" << endl;
cout << " 0. Quit" << endl;
cout << "Choose your option : ";
cin >> choice;
switch (choice)
{
case 1:
setPlayerData(playerList, NO_OF_BASEBALL_PLAYER);
break;
case 2:
getPlayerData(playerList, NO_OF_BASEBALL_PLAYER);
break;
case 3:
infile.open ("scores.txt");
if (!infile)
{
cout << "Cannot open the input file." << endl;
return 1;
}
getDataFromFile (infile, playerList, NO_OF_BASEBALL_PLAYER);
cout << "Player's information reloaded from file player's List successfully" << endl;
infile.close();
break;
case 4:
cout << "Enter Player's Name : ";
cin.ignore ();
getline (cin, name);
infile.open ("scores.txt");
if (!infile)
{
cout <<"Cannot open the input file." << endl;
return 1;
}
getDataFromFile (infile, playerList,NO_OF_BASEBALL_PLAYER);
index = searchPlayer
(playerList, NO_OF_BASEBALL_PLAYER, name);
if (index == NO_OF_BASEBALL_PLAYER)
cout << "Payer information doesn't exist in the Data File." << endl;
else
cout << "Player information does exist at " << index << " Position." << endl;
infile.close();
break;
case 5:
cout << "Enter Player's name whom information needs to be updated for :" << endl;
cin.ignore ();
getline (cin, name);
infile.open ("scores.txt");
if (!infile)
{
cout << "Cannot open the input file." << endl;
return 1;
}
getDataFromFile (infile, playerList, NO_OF_BASEBALL_PLAYER);
index = searchPlayer (playerList, NO_OF_BASEBALL_PLAYER, name);
if (index == NO_OF_BASEBALL_PLAYER)
cout << "Payer information doesn't exist in the Data File." << endl;
else
{
update(playerList, NO_OF_BASEBALL_PLAYER, index);
cout << "Player " << name << " has been updated succesfully " << endl;
}
infile.close();
break;
case 0:
char ch;
cout << "You selected Zero to quit the application." << endl;
cout << "If you want to save you data, type Y/y, otherwise N/n : ";
cin >> ch;
if (ch == 'Y' || ch == 'y')
{
outfile.open ("scores.txt");
saveData (outfile, playerList, NO_OF_BASEBALL_PLAYER);
cout << "Player's information saved to the file successfully" << endl;
outfile.close();
}
cout << "Exiting from Program";
return 0;
default:
cout << "You have Entered invalid input" << endl;
}
}while (choice != 0);
return 0;
}
void setPlayerData (baseBallPlayerType playerList[], int listSize)
{
for (int i = 0; i < listSize; i++)
{
cout << "Enter " << i+1 << " Player's Name : ";
cin.ignore();
getline (cin, playerList[i].playerName);
cout << "Enter " << playerList[i].playerName << "'s no.of home runs : ";
cin >> playerList[i].homeRuns;
cout << "Enter " << playerList[i].playerName << "'s no.of hits : ";
cin >> playerList[i].hits;
}
}
void getPlayerData (baseBallPlayerType playerList[], int listSize)
{
cout << "Player' Name No.of home Runs No.of Hits" << endl;
for (int i = 0; i < listSize; i ++)
cout << playerList[i].playerName << " " << playerList[i].homeRuns << " " << playerList[i].hits << endl;
}
void getDataFromFile (ifstream& infile, baseBallPlayerType playerList[], int listSize)
{
for (int i = 0; i < listSize; i ++)
{
infile >> playerList[i].playerName;
infile >> playerList[i].homeRuns;
infile >> playerList[i].hits;
}
}
void saveData (ofstream& outData, baseBallPlayerType playerList[], int listSize)
{
for (int i = 0; i < listSize; i ++)
{
outData << playerList[i].playerName << endl;
outData << playerList[i].homeRuns << endl;
outData << playerList[i].hits << endl;
}
}
int searchPlayer (baseBallPlayerType playerList[], int listSize, string name)
{
int index;
for (int i = 0; i < listSize; i ++)
if (playerList[i].playerName == name)
return i;
return listSize;
}
void update (baseBallPlayerType playerList[], int listSize, int index)
{
cout << "Enter " << playerList[index].playerName << "'s new home Runs :";
cin >> playerList[index].homeRuns;
cout << "Enter " << playerList[index].playerName << "'s new no.of hits :";
cin >> playerList[index].hits;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.