Answer to Question #194024 in C++ for Sankalp

Question #194024

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-18T02:54:46-0400
#include <iostream>
#include <string>
using namespace std;
class Player
{
public:
	Player() {};
	void set_data(int code, string name, int runs, int innings, int played, int outtime) 
	{
		p_code = code;
		p_name = name;
		p_runs = runs;
		p_innings = innings;
		p_played = played;
		p_outtime = outtime;
	}
	double get_average() 
	{
		p_average = 1.0*p_runs / p_outtime;
		return p_average;
	}
	void enter_data() 
	{
		int code;
		string name;
		int  runs;
		int innings;
	    int played;
		int outtime;
		cout <<"Enter player code: ";
		cin >> code;
		cout << "Enter player name: ";
		cin >> name;
		cout << "Enter player  runs: ";
		cin >> runs;
		cout << "Enter player  innings: ";
		cin >> innings;
		cout << "Enter player  played: ";
		cin >> played;
		cout << "Enter player  number of times not out: ";
		cin>> outtime;
		set_data(code, name, runs, innings, played, outtime);
	}
private:
	int p_code{ 0 };
	string p_name;
	int p_runs{ 0 };
	int p_innings{ 0 };
	int p_played{ 0 };
	int p_outtime{ 0 };
	double p_average{0};


};


int main()
{
	int choice{0};
	int count_players{ 0 };
	int player_number{ 0 };
	Player players[10];
	cout << "Menu:" << endl;
	cout << "1. Enter details of players." << endl;
	cout << "2. Display average runs of a single player" << endl;
	cout << "3.  Average runs of all players" << endl;
	cout << "4.  Exit" << endl;
	cout << "Make a selection enter the number corresponding to the menu item" << endl;
	cout << "The correctness of the entered data is entrusted to the user" << endl;
	do
	{
		cout << "your choice : ";
		cin >> choice;
		if (choice == 1) 
		{
			cout << "Enter the number of players: ";
			cin >> count_players;
			for (int i = 0; i < count_players; i++) 
			{
				cout << "Enter " << i + 1 << " player details " << endl;
				players[i].enter_data();
			}
		}
		else if (choice == 2) 
		{
			cout << "Enter the number of player: ";
			cin >> player_number;
			cout << "Player " << player_number << " Average runs: " << players[player_number - 1].get_average() << endl;
		}
		else if (choice == 3)
		{
			for (int i = 0; i < count_players; i++) 
			{
				cout << "Player " << i + 1 << " Average runs: " << players[i].get_average() << endl;
			}
		}
	} while (choice!=4);
	return 0;
    
}

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
APPROVED BY CLIENTS