Write necessary class and member function definitions for a cricket player object. (Use array of objects).
The program should accept details from user (max 10) : player code, name, runs, Innings, played, number of times not out.
The program should contain following menu:-
Enter details of players.
Display average runs of a single player.
Average runs of all players
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
class cricket{
public:
int playerCode;
string name;
int runs, innings, notOut;
int avg;
int out;
void input()
{
cout<<"Enter Player Code : ";
cin>>playerCode;
cout<<"Enter Player Name : ";
cin>>name;
cout<<"Enter total runs : ";
cin>>runs;
cout<<"Enter total Innings Played : ";
cin>>innings;
cout<<"Enter number of Times-Not-Out : ";
cin>>notOut;
out = innings-notOut;
}
void average()
{
int ans = runs/(innings-out);
avg = ans;
}
};
int main()
{
int n;
cout<<"Enter number of players : ";
cin>>n;
cricket c[n];
cout<<endl;
cout<<"Enter players details : "<<endl;
cout<<"----------------------------"<<endl;
for(int i=0; i<n; i++)
{
cout<<endl<<"Player"<<i+1<<endl;
c[i].input();
c[i].average();
cout<<endl;
}
string str;
cout<<"Enter name of player for knowing his average batting run : ";
cin>>str;
bool res = false;
for(int i=0; i<n; i++)
{
if(c[i].name == str)
{
res = true;
cout<<"Avg batting runs of "<<str<<" is : "<<c[i].avg<<endl;
}
}
if(res == false)
{
cout<<"Name of player entered not found"<<endl;
}
cout<<endl<<"Displaying Average Batting runs of Players"<<endl;
cout<<"------------------------------------------------------"<<endl;
cout<<endl<<"Player Name Avg Batting runs"<<endl;
for(int i=0; i<n; i++)
{
cout<<std::setw(10)<<c[i].name<<std::setw(10)<<c[i].avg<<endl;
}
}
Comments
Leave a comment