Question

In c++ Write a program that contains a class called Player. This class should contain two...

In c++

Write a program that contains a class called Player.
This class should contain two member variables: name, score.

Here are the specifications:
You should write get/set methods for all member variables.
You should write a default constructor initializes the member variables to appropriate default values.

Create an instance of Player in main.
You should set the values on the instance and then print them out on the console.

In Main

Declare a variable that can hold a dynamcially allocated array of Player.

Write code that will ask the user how big of an array of player that they want to create.
Once you get that input data create an array of that size.

Next write code that asks the user to enter data for every element of the array. Hint: Need a loop for this.

Modify the program

so that it will read in the Player data from a data file instead of standard input.
Ask the user to enter a filename and then read the data from that filename.
You will have to dynamically allocate an array based on the number of players indicated in the file.


File Format

#OfPlayers
PlayerName PlayerScore

PlayerName PlayerScore

Example File

2
Derek 100
Alex 90

0 0
Add a comment Improve this question Transcribed image text
Answer #1


#include <iostream>

using namespace std;

// Creating player class
class Player
{
private:
// Declaring variables
string name;
int score;

public:
Player();
// Function declarations
string getName();
void setName(string name);
int getScore();
void setScore(int score);
};

// Zero argumented Constructor
Player::Player()
{
this->name = "";
this->score = 0;
}

// Function Implementations
string Player::getName()
{
return name;
}
void Player::setName(string name)
{
this->name = name;
}
int Player::getScore()
{
return score;
}
void Player::setScore(int score)
{
this->score = score;
}
int main()
{
// Declaring variables
string name;
int score, size;

// Getting the Size of an array
cout << "Enter the Size of an array :";
cin >> size;

// Creating Player array dynamically based on size
Player* arr = new Player[size];

/* getting the values entered by the user
* and populating those into an array
*/
for (int i = 0; i < size; i++)
{
// getting the inputs entered by the user
cout << "Enter the name of Player#" << i + 1 << ":";
cin >> name;
arr[i].setName(name);
cout << "Enter the Score :";
cin >> score;
arr[i].setScore(score);
}

// Displaying the output
cout << "\nPlayer Name\tScore" << endl;
cout << "-----------\t-----" << endl;
for (int i = 0; i < size; i++)
{
cout << arr[i].getName() << "\t\t" << arr[i].getScore() << endl;
}

return 0;
}

___________________

Output:

__________________

Read data from File

playersInfo.txt (Save this file under D Drive.Then the path of the file pointing to it is D:\\playersInfo.txt)

2
Derek 100
Alex 90
_____________________

Program:

#include <fstream>
#include <iostream>
#include <iomanip>

using namespace std;

// Creating player class
class Player
{
private:
// Declaring variables
string name;
int score;

public:
Player();
// Function declarations
string getName();
void setName(string name);
int getScore();
void setScore(int score);
};

// Zero argumented Constructor
Player::Player()
{
this->name = "";
this->score = 0;
}

// Function Implementations
string Player::getName()
{
return name;
}
void Player::setName(string name)
{
this->name = name;
}
int Player::getScore()
{
return score;
}
void Player::setScore(int score)
{
this->score = score;
}
int main()
{
   //defines an input stream for the data file  
ifstream dataIn;
  
  
// Declaring variables
string name;
int score, size;
string filename;

//Getting the name of the file
cout<<"Enter the filename :";
cin>>filename;

//Opening the input file
dataIn.open(filename.c_str());
if(dataIn.fail())
{
   cout<<"** File not found **"<<endl;
   return 1;
}
else
{
   dataIn>>size;
  
// Creating Player array dynamically based on size
Player* arr = new Player[size];

/* getting the values from file
* and populating those into an array
*/
for (int i = 0; i < size; i++)
{
// getting the inputs from file
dataIn>>name>>score;
arr[i].setName(name);
arr[i].setScore(score);
}

// Displaying the output
cout << "\nPlayer Name\tScore" << endl;
cout << "-----------\t-----" << endl;
for (int i = 0; i < size; i++)
{
cout << arr[i].getName() << "\t\t" << arr[i].getScore() << endl;
}
dataIn.close();
  
}

return 0;
}

_________________________

Output:

CProgram Files (x86)\Dev-Cpp MinGW64vbin\ReadFilePlayerinfoAndCreatePlay Enter the filename DaNplayersInfo.txt Player Name De

_______________Thank You

Add a comment
Know the answer?
Add Answer to:
In c++ Write a program that contains a class called Player. This class should contain two...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C++ Write a program that contains a class called VideoGame. The class should contain the...

    In C++ Write a program that contains a class called VideoGame. The class should contain the member variables: Name price rating Specifications: Dynamically allocate all member variables. Write get/set methods for all member variables. Write a constructor that takes three parameters and initializes the member variables. Write a destructor. Add code to the destructor. In addition to any other code you may put in the destructor you should also add a cout statement that will print the message “Destructor Called”....

  • Create a class named Game using the following UML class diagram. GAME -gameName : string +Game()...

    Create a class named Game using the following UML class diagram. GAME -gameName : string +Game() +setGameName(in name : string) : void +getGameName() : string +displayMessage() : void This class has 2 member variables namely playerName and playerScore. The class contain following member functions: Constructor, set function, get functions, display function. Code write in 3 files: Game.h header file, funtion.cpp file and main.cpp file. The output should be: Player John has scored 50 points.

  • In C++ Write a program that contains a BankAccount class. The BankAccount class should store the...

    In C++ Write a program that contains a BankAccount class. The BankAccount class should store the following attributes: account name account balance Make sure you use the correct access modifiers for the variables. Write get/set methods for all attributes. Write a constructor that takes two parameters. Write a default constructor. Write a method called Deposit(double) that adds the passed in amount to the balance. Write a method called Withdraw(double) that subtracts the passed in amount from the balance. Do not...

  • Visual C# Homework 2 You are to write a program which will create a class called...

    Visual C# Homework 2 You are to write a program which will create a class called Student Each student has a name age height and weight. These variables should be declared as private. Create the correct constructors and functions. In the main, you will create 5 students. Input the data for the 5 students from a file which already has information in it. Name the file “Information.txt”. After setting up all the data, it is time to sort based on...

  • FOR C++ PLEASE Create a class called "box" that has the following attributes (variables): length, width,...

    FOR C++ PLEASE Create a class called "box" that has the following attributes (variables): length, width, height (in inches), weight (in pounds), address (1 line), city, state, zip code. These variables must be private. Create setter and getter functions for each of these variables. Also create two constructors for the box class, one default constructor that takes no parameters and sets everything to default values, and one which takes parameters for all of the above. Create a calcShippingPrice function that...

  • *** C++ *** Create a class called Student that contains a first name ( string) and...

    *** C++ *** Create a class called Student that contains a first name ( string) and an student id number (type long).     1. Include a member function called get_data( ) to get data from the user for insertion into the object,     2. Also include a function called display_data( ) that displays the student data. Then write a complete program that uses the class to do some computation. Your main function, main( ), should create a Dynamic Array of...

  • (C++ Program) 1. Write a program to allow user enter an array of structures, with each...

    (C++ Program) 1. Write a program to allow user enter an array of structures, with each structure containing the firstname, lastname and the written test score of a driver. - Your program should use a DYNAMIC array to make sure user has enough storage to enter the data. - Once all the data being entered, you need to design a function to sort the data in descending order based on the test score. - Another function should be designed to...

  • This is JAVA language Create a .java class called MoreArrayFun.java with only a main method. This...

    This is JAVA language Create a .java class called MoreArrayFun.java with only a main method. This program will use the ArrayManager class. The final version of the program is described below, but initially use it to test your ArrayManager class as you write it. Complete the ArrayManager class as specified below: The class will have exactly two instance variables:  An array of integers  A count of the number of elements currently in the array The class will have...

  • What to submit: your answers to exercises 2. Write a Java program to perform the following...

    What to submit: your answers to exercises 2. Write a Java program to perform the following tasks: The program should ask the user for the name of an input file and the name of an output file. It should then open the input file as a text file (if the input file does not exist it should throw an exception) and read the contents line by line. It should also open the output file as a text file and write...

  • Write a complete C++ program that defines the following class: Class Salesperson { public: Salesperson( );...

    Write a complete C++ program that defines the following class: Class Salesperson { public: Salesperson( ); // constructor ~Salesperson( ); // destructor Salesperson(double q1, double q2, double q3, double q4); // constructor void getsales( ); // Function to get 4 sales figures from user void setsales( int quarter, double sales); // Function to set 1 of 4 quarterly sales // figure. This function will be called // from function getsales ( ) every time a // user enters a sales...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT