program for information for historical baseball players
Question
In this program the user will enter the following information for historical
baseball players (up to the maximum number of players indicated by MAX_NUM_OF_PLAYERS):
The player’s name
The number of hits they got in their career
The number of at bats during their career
batting average = # of hits <divided by> # of at bats
#include<iostream> #include<iomanip> using namespace std; cout << endl; cout << "PLAYER AVG" << endl; cout << "=================" << endl; cout << left << fixed << setprecision(3); //for (initialize; test; increment) { /* TODO - Step 13 Calculate the player's batting average by calling the BattingAverage function /* Sample Output Enter Player #1 stats: Name: Aaron Total Hits: 3771 Total At Bats: 12364 Enter another player (y/n): y Enter Player #2 stats: Name: Cobb Total Hits: 0 Reenter Hits: 4189 Total At Bats: 11434 Enter another player (y/n): y Enter Player #3 stats: Name: Gehrig Total Hits: 2721 Total At Bats: 801 Reenter At Bats: 8001 Enter another player (y/n): y Enter Player #4 stats: Name: Ruth Total Hits: 2873 Total At Bats: 8398 Enter another player (y/n): n PLAYER AVG ================= Aaron 0.305 Cobb 0.366 Gehrig 0.340 Ruth 0.342 Batting champion: Cobb, 0.366 career average! Press any key to continue . . . */
Summary
Here in the given question user have to write information for historical baseball players. Which includes The player’s name and The number of hits they got in their career and The number of at-bats during their career. And Finally, it will get printed on the screen.
Explanation
Basically here the information of the historical baseball players is taken. For that, we used an input function. We take all the information from the user. We will take the name. the number of hits, total hits on bats, and is there any other player information is to be taken. For that, we have placed two options ‘y’ and ‘n’. y for yes and n for no. If the user enters y it will again ask for the information of another player. And if the user enters n then the program will get terminated. Also average of the players will get points on the screen as output.
Code
#include <iostream> using namespace std; #define MAX_NUM_OF_PLAYERS 10 #include <iomanip> /* to get the batting average from the total hits and at bats */ float BattingAverage(int hits,float bats) { return static_cast<float>(hits/bats); } int main() { /* to store all the player names */ string playerName[MAX_NUM_OF_PLAYERS]; /* to store all the batting averages of all players */ float avg[MAX_NUM_OF_PLAYERS]; int cc=0; while(true){ /* to get the data from user */ cout<<"Enter player #"<<(cc+1)<<" stats:"<<endl; cout<<"Name: "; cin>>playerName[cc]; cout<<"Total Hits: "; int hits; cin>>hits; /* if the hits are invalid */ while(hits==0){ cout<<"Reenter Hits: "; cin>>hits; } cout<<"Total At Bats: "; float totalBats; cin>>totalBats; /* if the total at bats are invalid */ while(totalBats<hits){ cout<<"Reenter At Bats: "; cin>>totalBats; } avg[cc]=BattingAverage(hits,totalBats); cc++; cout<<"Enter another player (y/n): "; char choice; cin>>choice; if(choice=='n') break; cout<<endl; } /* to display the player name and his batting average */ cout<<endl<<endl; cout<<left; cout<<setw(15)<<"PLAYER"<<setw(15)<<"AVG"<<endl; for(int i=0;i<30;i++) cout<<"="; cout<<endl; for(int i=0;i<cc;i++) cout<<setw(15)<<playerName[i]<<setw(15)<< fixed<<setprecision(3)<<avg[i]<<endl; cout<<endl; float large=0; string player; /* to find the player with the highest batting average */ for(int i=0;i<cc;i++){ if(avg[i]>large){ large=avg[i]; player=playerName[i]; } } cout<<"Batting champion: "<<player<<", "<<large<<" career average!"<<endl; }
Output
Also, read a program for information for historical baseball players.