Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help me with this activity. Thank you The activities you need to complete today are as follows: If you like, your group can start

Please help me with this activity. Thank you

The activities you need to complete today are as follows:

If you like, your group can start with your project from Lab 2 yesterday. If you make a new project, then you will need to copy your player.h and player.cpp files over to your new project. I would recommend that you just work from yesterdays Visual Studio project, comment out the old code in main.cpp (so that you still have it to look at), and write the code for this project in your old main.cpp file.

Here is Lab 2

#include using namespace std; class Player { private: string name; int age; public: Player() //Constructor { name=""; age=0; } int getAge() //Accessor { return age; } string getName() //Accessor { return name; } void setAge(int a) //Mutators { age=a; } void setName(string n) //Mutators { name=n; } void display() { cout<<"Player name is "<>n; cout<<"Enter age: "; cin>>a; setName(n); setAge(a); } }; int number_of_players; void printPlayers(Player *arr,int number_of_players) { int i; for(i=0;i>choice; if(choice==1) { cout<<"Enter new name: "; cin>>new_name; } else { cout<<"Enter new age: "; cin>>new_age; } break; } } if(i==number_of_players) { cout<<"Player with name "<>number_of_players;

for(i=0;i>n; modifyPlayer(arr,n); cout<<"Enter the name of the player you wish to modify: "; cin>>n; modifyPlayer(arr,n); printPlayers(arr,number_of_players); }

1. The first thing youll need to do is to make a text file to read. You can do this by right clicking on the folder Resource Files in the Solution Explorer, and then selecting Add->New Item. In the box that comes up select Text File (.txt). Give your file a name, keep the default Location, and then click the Add button. Type 10 or 20 first names only into this file (one per line) and save it.

2. Write a function called chooseRandomName that takes no parameters and returns a string. This function should open the text file that you just made in step 1, read all of the names from this file, randomly choose one of the names, close the file, and then return the chosen name. Have your main function call the chooseRandomName function and print the value it returns to the screen to verify that it works.

3. Write a function called randomlyGeneratePlayer. This function should take as parameters an array of type Player (the class you wrote Wednesday) and an array index. The function should choose a random name (by calling the function you wrote in part 2), and randomly choose a value for age. These randomly chosen values should be placed into the object in the Player array at the index passed into the function. To verify that the function works, have your main function call the randomlyGeneratePlayer function and call the print method of the object whose values were randomly generated.

4. Write a function called savePlayerList. This function should take as a parameter an array of type Player and an integer called totalPlayers (that indicates the number of players in the array). This function should begin by asking the user for the name of a file to save to, open the file, and then write the totalPlayers as the first value in the file. The function should then write all of the data about each player in the array, one piece of data per line. The function should then close the file. To verify that your function works, call your randomlyGeneratePlayer function from part 3 at least 10 times to fill your Player array with some data. Then, call savePlayerList and make sure that the function does indeed create the text file with the data formatted appropriately.

5. Write a function called readPlayerList. This function should take as a parameter an array of type Player and return an integer. This function should begin by asking the user for the name of a file to open, open the file, and then read the contents of the file into the Player array that was received as a parameter. The function should then close the file and return the total number of players that were read from the file. To verify that your function works, have main call readPlayerList, and type in the name of the file that you made in part 4. Then, call the printPlayers function that you made during the last lab and pass it the Player array and the total number of players returned by the readPlayerList function.

OPTIONAL: Modify the chooseRandomName and readPlayerList functions so that they work with first and last names on the same line.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

3. Are our bosses always right? If not, what should we do?

Answered: 1 week ago

Question

2. What, according to Sergey, was strange at this meeting?

Answered: 1 week ago