Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

home / study / engineering / computer science / computer science questions and a

ID: 3751203 • Letter: H

Question

home / study / engineering / computer science / computer science questions and answers / c++ a menu-driven program gives the user the option to find statistics about different baseball ...

Question: C++ A menu-driven program gives the user the option to find statistics about different baseball p...

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

Screenshot

Program

//header files
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
//Struct to read player details
struct player_detail
{
   string team_name;
   string player_name;
   int number_home_run;
   double avg;
   int runs_batted;
};
//Struct array
player_detail pd[10];
//Function prototype
void fileread();
void menu();
void printAll();
void printPlayer(string);
void printTeam(string);
void update(string);
void save();
//Main method
int main()
{
   //Varibles for read and operations
   int ch;
   string pname, tname,combinename="";
   //Call file read to get details
   fileread();
   //call menu function
   menu();
   //prompt user choice
   cout << "Enter your choice: ";
   cin >> ch;
   //Check choice correctness
   while (ch < 1 || ch>5) {
       cout << "Please enter the choice between 1-5 !!!" << endl;
       menu();
       cout << "Enter your choice: ";
       cin >> ch;
   }
   //According to the choice operations
   while (ch < 5) {
       switch(ch) {
           //Print all player details
       case 1:
           printAll();
           break;
           //Print particular player details
       case 2:
           combinename = "";
           cin.ignore();
           cout << "Enter Player name:";
           getline(cin,pname);
           for (int i = 0; i < pname.length(); i++) {
               if(pname[i] != ' ') {
                   combinename += pname[i];
               }
           }
           printPlayer(combinename);
           break;
           //Print particular team details
       case 3:
           combinename = "";
           cin.ignore();
           cout << "Enter Team name:";
           getline(cin,tname);
           for (int i = 0; i < tname.length(); i++) {
               if (tname[i] != ' ') {
                   combinename += tname[i];
               }
           }
           printTeam(combinename);
           break;
           //Update a player
       case 4:
           combinename = "";
           cin.ignore();
           cout << "Enter Player name:";
           getline(cin, pname);
           for (int i = 0; i < pname.length(); i++) {
               if (pname[i] != ' ') {
                   combinename += pname[i];
               }
           }
           update(combinename);
           break;
       }
      
       cout << "Enter your choice: ";
       cin >> ch;
      
       while (ch < 1 || ch>5) {
           cout << "Please enter the choice between 1-5 !!!" << endl;
           menu();
           cout << "Enter your choice: ";
           cin >> ch;
       }
   }
   char c;
   //Prompt for store
   cout << "Do you want to save the details(y/n): ";
   cin >> c;
   //If yes then store into a file
   if (c == 'y' || c == 'Y') {
       save();
   }
   //Otherwise exit
   else {
       cout << "Goodbye!!!!" << endl;
   }
    return 0;
}
//File read function definition
void fileread() {
   ifstream in;
   string w1, w2, w3, w4, w5;
   int i = 0;
   in.open("C:/Users/deept/Desktop/baseball.txt");
   if (!in) {
       cout << "File not found" << endl;
       exit(0);
   }
   else {
       while (!in.eof()) {
           in >> pd[i].team_name >> pd[i].player_name >> pd[i].number_home_run >> pd[i].avg >> pd[i].runs_batted;
           i++;
       }
   }
   in.close();
}
//menu display function
void menu() {
   cout << "1. print out all users and statistics 2. print out the statistics for a specific player 3. print out all data for a specific team 4. update the data for a particular player 5. Exit ";
}
//Print all player details
void printAll() {
   for (int i = 0; i < 10; i++) {
       cout << pd[i].team_name << " " << pd[i].player_name << " " << pd[i].number_home_run << " " << pd[i].avg << " " << pd[i].runs_batted << endl;
   }
}
//Print particular player details
void printPlayer(string s) {
   for (int i = 0; i < 10; i++) {
       if (pd[i].player_name == s) {
           cout << pd[i].team_name << " " << pd[i].player_name << " " << pd[i].number_home_run << " " << pd[i].avg << " " << pd[i].runs_batted << endl;
           return;
       }
      
   }
}
//Print particular team details
void printTeam(string s) {
   for (int i = 0; i < 10; i++) {
       if (pd[i].team_name == s) {
           cout << pd[i].team_name << " " << pd[i].player_name << " " << pd[i].number_home_run << " " << pd[i].avg << " " << pd[i].runs_batted << endl;
       }

   }
}
//update player details
void update(string s) {
   string tname;
  
   for (int i = 0; i < 10; i++) {
       if (pd[i].player_name == s) {
           string combinename1 = "";
          
           cout << "Enter Team name:";
           getline(cin, tname);
           for (int j = 0; j < tname.length(); j++) {
               if (tname[j] != ' ') {
                   combinename1 += tname[j];
               }
           }
           pd[i].team_name = combinename1;
           cout << "Enter number of home runs: ";
           cin >> pd[i].number_home_run;
          
           cout << "Enter average: ";
           cin >> pd[i].avg;
          
           cout << "Enter batted run: ";
           cin >> pd[i].runs_batted;
           ofstream out;
          
           out.open("C:/Users/deept/Desktop/baseball.txt");
           if (!out) {
               cout << "File not found" << endl;
               exit(0);
           }
           else {
              
               for (int i = 0; i < 10; i++) {
                   out << pd[i].team_name << " " << pd[i].player_name << " " << pd[i].number_home_run << " " << pd[i].avg << " " << pd[i].runs_batted << endl;
               }
           }
           out.close();
           cout << "updation completed!!!!" << endl;
           return;
       }

   }
}
//Save into a file
void save() {
   ofstream out;
   out.open("C:/Users/deept/Desktop/baseballout.txt");
   if (!out) {
       cout << "File can't open!!!" << endl;
       exit(0);
   }
   else {
      
           for (int i = 0; i < 10; i++) {
               out << pd[i].team_name << " " << pd[i].player_name << " " << pd[i].number_home_run << " " << pd[i].avg << " " << pd[i].runs_batted << endl;
           }
      
   }
}

------------------------------------------------

Output

1. print out all users and statistics
2. print out the statistics for a specific player
3. print out all data for a specific team
4. update the data for a particular player
5. Exit
Enter your choice: 2
Enter Player name:Mike Trout
AmericanLeague MikeTrout 5 0.314 30
Enter your choice: 5
Do you want to save the details(y/n): n
Goodbye!!!!
Press any key to continue . . .

My file

AmericanLeague MookieBetts 5 .334 36
AmericanLeague MikeTrout 5 .317 28
NationalLeague ChristianYelich 5 .319 31