Question

c++ A menu-driven program gives the user the option to find statistics about different baseball players....

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

// The main changes made are changing int batting_avg to double batting_avg so complete data will be displayed instead of all zeroes

// i have changed int choice to char choice

// storeData() i have run a loop containing all data stored in Object structure to be stored in outFile reference

#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

const int SIZE = 10;

struct player { //structure of player
int id;
string team;
string name;
int homeruns;
double batting_avg;
int runs;
}object[SIZE]; //reference of array of structure

void loadData();
int loadMenu(int &);
void printAll();
void printSpecific(int );
void printSpecificTeam(string );
void updatePlayer(int , string );
int storeData();

int main() {
char choice; //choice should be taken as char type instead of int
int option,idPlayer, k;
string teamName;
loadData();
do { //menu to be displayed
loadMenu(option);

switch (option) { //different options methods
case 1: printAll();
break;
case 2:
cout << "enter id";
cin >> idPlayer;
printSpecific(idPlayer);
break;
case 3:
cout << "enter team";
cin >> teamName;
printSpecificTeam(teamName);
break;
case 4:
cout << "enter id and team you wnat to update";
cin >> idPlayer >> teamName;
updatePlayer(idPlayer, teamName);
break;
case 5:
k = storeData();
if (k == 1)
cout << "Successfully stored";
else
cout << "error. not stored";
break;
case 6: exit(0);
break;
default: cout << "Oops something went wrong!";
}
cout << "Continue?(Y/N)";
cin >> choice;
} while (choice == 'y' || choice == 'Y');
}

void loadData() { //read data from file
ifstream in("player.txt");
int count = 0;
if (!in)
{
cout << "File can't be opened! " << endl;
}
while (!in.eof() && count<SIZE) {
in >> object[count].id >> object[count].team >> object[count].name >> object[count].homeruns >> object[count].batting_avg >> object[count].runs;
++count;
}

}
int loadMenu(int &option) {
do {
cout << "Enter your choice\n" <<
"1. Print out all players and statistics\n" <<
"2. Print out the statistics for a specific player\n" <<
"3. Print out all data for specific item\n" <<
"4. Update the data for a particular player\n" <<
"5. Store data in file\n" <<
"6. Exit\n";
cin >> option;
if (option < 0 || option > 7) {
cout << "Invalid selection. Try again.\n";
}
} while (option < 0 || option > 7);
return option;
};

void printAll() { //print data after reading from file
for (int i = 0; i < SIZE; i++)
{
cout << object[i].id << " ";
cout << object[i].team << " ";
cout << object[i].name << " ";
cout << object[i].homeruns << " ";
cout << object[i].batting_avg << " ";
cout << object[i].runs << " " << endl;
}

}

void printSpecific(int idp) { //print data of specific player

for (int i = 0; i<SIZE; ++i) {
if (object[i].id == idp) {
cout << object[i].id << " ";
cout << object[i].team << " ";
cout << object[i].name << " ";
cout << object[i].homeruns << " ";
cout << object[i].batting_avg << " ";
cout << object[i].runs << " " << endl;
break;
}
}

}
void printSpecificTeam(string teamName) { //print data of specific team

for (int i = 0; i<SIZE; ++i) {
if (object[i].team == teamName) {
cout << object[i].id << " ";
cout << object[i].team << " ";
cout << object[i].name << " ";
cout << object[i].homeruns << " ";
cout << object[i].batting_avg << " ";
cout << object[i].runs << " " << endl;
}
}
}

void updatePlayer(int idp, string teamName) { //update particular player team by searching it through id
for (int i = 0; i<SIZE; ++i) {
if (object[i].id == idp) {
object[i].team = teamName;
break;
}
}
}

int storeData() { //store data in file
ofstream outFile;
outFile.open("player.txt");
if (!outFile)
{
cout << "File can't be opened! " << endl;
}
for(int count=0;count<SIZE;++count)
{
outFile<< object[count].id <<" "<<object[count].team <<" "<<object[count].name << " "<<object[count].homeruns <<" "<<object[count].batting_avg <<" "<< object[count].runs<<endl;
}

return 1;

}

output:

Add a comment
Know the answer?
Add Answer to:
c++ A menu-driven program gives the user the option to find statistics about different baseball players....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. You will develop a Python program to manage information about baseball players. The program will...

    1. You will develop a Python program to manage information about baseball players. The program will maintain the following information for each player in the data set: player’s name (string) team identifier (string) games played (integer) at bats (integer) runs scored (integer) hits (integer) doubles (integer) triples (integer) homeruns (integer) batting average (real) slugging percentage (real) The first nine items will be taken from a data file; the last two items will be computed by the program. The following formulas...

  • Need help writing this code in python. This what I have so far. def display_menu(): print("======================================================================")...

    Need help writing this code in python. This what I have so far. def display_menu(): print("======================================================================") print(" Baseball Team Manager") print("MENU OPTIONS") print("1 - Calculate batting average") print("2 - Exit program") print("=====================================================================")    def convert_bat(): option = int(input("Menu option: ")) while option!=2: if option ==1: print("Calculate batting average...") num_at_bats = int(input("Enter official number of at bats: ")) num_hits = int(input("Enter number of hits: ")) average = num_hits/num_at_bats print("batting average: ", average) elif option !=1 and option !=2: print("Not a valid...

  • write a C# program using replit that uses a loop to input # at bats and # of hits for each of the 9 baseball players on...

    write a C# program using replit that uses a loop to input # at bats and # of hits for each of the 9 baseball players on a team. Team name, player name, and player position will also be input.(two dimensional array) The program should then calculate and print the individual batting average and, after all 9 players have been entered, calculate and print the team batting average. Example of inputs and Outputs... Enter the Team name: (one time entry)...

  • (C++) Write a program that declares a struct to store the data of a football player...

    (C++) Write a program that declares a struct to store the data of a football player (player’s name, player’s position, number of touchdowns, number of catches, number of passing yards, number of receiving yards, and the number of rushing yards). Declare an array of 10 components to store the data of 10 football players. Your program must contain a function to input data and a function to output data. Add functions to search the array to find the index of...

  • PLEASE INCLUDE SAW-PROMPTS FOR 2 PLAYERS NAMES(VALIDATE NAMES). SHOW MENU (PLAYER MUST SELECT FROM MENU B4...

    PLEASE INCLUDE SAW-PROMPTS FOR 2 PLAYERS NAMES(VALIDATE NAMES). SHOW MENU (PLAYER MUST SELECT FROM MENU B4 THE GAME STARTS 1=PLAY GAME, 2=SHOW GAME RULES, 3=SHOW PLAYER STATISTICS, AND 4=EXIT GAME WITH A GOODBYE MESSAGE.) PLAYERS NEED OPTION TO SHOW STATS(IN A DIFFERNT WINDOW-FOR OPTION 3)-GAME SHOULD BE rock, paper, scissor and SAW!! PLEASE USE A JAVA GRAPHICAL USER INTERFACE. MUST HAVE ROCK, PAPER, SCISSORS, AND SAW PLEASE This project requires students to create a design for a “Rock, Paper, Scissors,...

  • Chapter 8 Python Case study Baseball Team Manager For this case study, you’ll use the programming...

    Chapter 8 Python Case study Baseball Team Manager For this case study, you’ll use the programming skills that you learn in Murach’s Python Programming to develop a program that helps a person manage a baseball team. This program stores the data for each player on the team, and it also lets the manager set a starting lineup for each game. After you read chapter 2, you can develop a simple program that calculates the batting average for a player. Then,...

  • Java 7.17 Clone of LAB*: Program: Soccer team roster This program will store roster and rating...

    Java 7.17 Clone of LAB*: Program: Soccer team roster This program will store roster and rating information for a soccer team. Coaches rate players during tryouts to ensure a balanced team. (1) Prompt the user to input five pairs of numbers: A player's jersey number (0-99, but this is NOT enforced by the program nor tested) and the player's rating (1-9, not enforced like jersey numbers). Store the jersey numbers in one int array and the ratings in another int...

  • In C Program This program will store the roster and rating information for a soccer team. There w...

    In C Program This program will store the roster and rating information for a soccer team. There will be 3 pieces of information about each player: Name: string, 1-100 characters (nickname or first name only, NO SPACES) Jersey Number: integer, 1-99 (these must be unique) Rating: double, 0.0-100.0 You must create a struct called "playData" to hold all the information defined above for a single player. You must use an array of your structs to to store this information. You...

  • Write a C++ menu driven Payroll program. Must be user friendly.   Menu 1. Check data file  ...

    Write a C++ menu driven Payroll program. Must be user friendly.   Menu 1. Check data file   2. Read Data file 3. Process payroll for one employee 4. Process payroll for all employees 5. Print out to a text or an HTML file 6. Exit You must use the following Payroll classes, structures, pointers, arrays, enum, vector, recursive, advance file I/O, STL, iterators and containers. The program to process an input file below to calculate tax of 28% and output it...

  • Write a contacts database program that presents the user with a menu that allows the user...

    Write a contacts database program that presents the user with a menu that allows the user to select between the following options: (In Java) Save a contact. Search for a contact. Print all contacts out to the screen. Quit If the user selects the first option, the user is prompted to enter a person's name and phone number which will get saved at the end of a file named contacts.txt. If the user selects the second option, the program prompts...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT