Answer to Question #193767 in C++ for Sankalp

Question #193767

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


1
Expert's answer
2021-05-15T17:19:52-0400
#include<iostream>
#include<conio.h>
using namespace std;
class cricket
{
    public:
        double average;
        int playerCode,runs,numberOfInnings;
        int numberOfTimesNotOut;
        char playerName[20];
    public:
        void get()
        {
            cout<<"\nEnter the details of player: \n";
            cout<<"Playercode Name Runs NumberOfInnings NumberOfTimesNotOut. (Separate each with space) \n";
            cin>>playerCode>>playerName>>runs>>numberOfInnings>>numberOfTimesNotOut;
        }
        void cal()
        {
            average=(runs / numberOfInnings);
        }
        void display(int n)
        {
            cout<<"\nDetails of single player: \n";
            cout<<"\nplayerCode\tname\trun\tno.Inning\tnot out\tAve.run\n";
            cout<<"==========================================================\n";
            cout<<n<<"\t"<<playerName<<"\t"<<runs<<"\t"<<numberOfInnings<<"\t\t"<<numberOfTimesNotOut<<"\t"<<average;
        }
        void display()
        {
            cout<<playerCode<<"\t"<<playerName<<"\t"<<runs<<"\t"<<numberOfInnings<<"\t"<<numberOfTimesNotOut<<"\t"<<average<<endl;
        }
};
int main()
{
    int n,i,ch;
    cricket c[10];
    //clrscr();
    do
    {
        cout<<"\n\tOPTIONS:\n";
        cout<<"\n1.Enter Details \n2.Display single Player\n3.Display all Players\n4.Quit";
        cout<<"\n\nEnter youer Choice: ";
        cin>>ch;
        switch(ch)
        {
            case 1:
                cout<<"\nEnter number of players to enter: \n";
                cin>>n;
                for(i=0;i<n;i++)
                {
                    c[i].get();
                    c[i].cal();
                }
                break;
            case 2:
                cout<<"\nWhich player Average run display: ";
                cout<<"\nEnter the player code: \n";
                cin>>n;
                for(i=0;i<n;i++)
                {
                    if(c[i].playerCode==n)
                    {
                    c[i].display(n);
                    }
                }
                break;
            case 3:
                cout<<"\nDetails of single player: \n";
                cout<<"\nPlayer Code\tName\tRuns\tNo. of Inning\tNot out\tAve.run\n";
                cout<<"===========================================\n";
                for(i=0;i<n;i++)
                {
                    c[i].display();
                }
                break;
            case 4:
            break;
        }
    }
    while(ch!=4);
        getch();
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog