Question: Can you guys help me with option 2 ( Open file ) and I am not sure with ( 5) Save Player list) ---------------------------------- Description:

Can you guys help me with option 2 ( Open file ) and I am not sure with ( 5) Save Player list)

----------------------------------

Description:

First, you need to create a class called Player. This class should contain the following attributes:

1) name (a string) 2) age (an integer)

This information should only be accessible through methods of the Player class.

First we have constructors. You must write one default constructor for the Player class.

Second are the access methods. These allow you to get and set members of the class. You must write one setter method and one getter method for each of the attributes.

Third we have the functionality methods. These are methods that allow our class to perform additional actions. For example, you should write a method to print the information about a player to the screen.

Your program should begin with the following menu:

1) New File 2) Open File 3) Quit

If the user selects option 1, just have the program proceed to the second menu (see below). If the user selects option 2, you should ask for a file name to open and read the contents of that file into an array of type Player (assume there will never be more than 20 players in any file). The file format will be explained below. The program should then proceed to the next menu. Option 3 should allow the program to end. If the user did not select option 3 from the first menu, then bring up the second menu:

1) Print all Players 2) Create a new Player 3) Randomly generate a new Player 4) Modify a Player 5) Save Player list 6) Close File

If the user selects option 1 from this menu, then your program should display all of the Players' information. If the user selects option 2, then you should ask for all the data about a Player (name and age) and put this new player into the array at the next available spot. If the user selects option 3, then you should have your program randomly create a Player by randomly choosing a name and an age. This Player should be placed in the next available spot in the array. If the user selects option 4, ask for the name of the Player they'd like to modify. If the Player is in the array, then ask what attribute they'd like to modify (name or age), get the new value, and then set the Player's attribute to this new value. If the player does not exist, give an error message. Option 5 should save the player data in the format specified below to a file name specified by the user. Option 6 should return the program to the first menu. NOTE: When adding new Players to the array, never allow the total number to go over 20. Give an error message if the user tries to do this.

THE DATA FILE:

The data file must take the format described here. You can assume there will never be more than 20 Players in a file. Data is organized such that there is only one piece of information per line. The first line of the file tells us how many Players there are. The next 2 lines of the file are the information about the first player. As described above, players have a name and an age. Following the data about the first player is the data about the second, and so on until all players have been specified. Here is a sample data file:

3 Martian 14 Astro 873 Alien 52

----------------------------------

Here is my code that i am working on

#include

#include

#include

#include

using namespace std;

int playernow = 0;

class Player{

public:

Player() //default constructor

{

name = "";

age = 0;

}

void Setplayername(string names)

{

name = names;

}

void Setplayerage(int ages)

{

age = ages;

}

string Getplayername()

{

return name;

}

int Getplayerage()

{

return age;

}

void print() {

// if (age != 0)

// {

cout << "Player name is " << name << " and age is " << age<

// }

}

void save(string name)

{

std::ofstream outfile(name);

outfile << name << std::endl;

outfile << age << std::endl;

outfile.close();

}

private:

string name;

int age;

};

void menu1(Player *arr)

{

int answer;

string addname;

int addage;

start:

{

cout << " ---------------------------- 1) Print all Players 2) Create a new Player 3) Randomly generate a new Player 4) Modify a Player 5) Save Player list 6) Close File ";

cin >> answer;

if (answer == 1)

{

if (playernow == 0)

{

cout << "You need to add the player!" << endl;

goto start;

}

else

{

cout << "Print all Players: " << endl;

for (int i = 0; i < playernow; i++)

{

arr[i].print();

}

goto start;

}

}

if (answer == 2)

{

if (playernow == 20)

{

cout << "You have 20 players, you cannot add more!" << endl;

goto start;

}

else

{

cout << "Create a new Player: " << endl;

cout << "Add the name for player: ";

cin >> addname;

cout << "Add the age of player: ";

cin >> addage;

arr[playernow].Setplayername(addname);

arr[playernow].Setplayerage(addage);

playernow += 1;

goto start;

}

}

if (answer == 3)

if (playernow == 20)

{

cout << "You have 20 players, you cannot add more!" << endl;

goto start;

}

else

{

string randomName[10] = { "Naruto", "Kakashi","Sakura","Sasuke","Mitsuki","Orochimaru","Boruto","Mitsuki","Goku","Yamato"};

int randomN = rand() % 9; //random 0-9

int randomA = rand() % 70 + 1; // random 0-70

addname = randomName[randomN];

addage = randomA;

cout << "The random name is: " << addname << endl;

cout << "The random age is: " << addage << endl;

arr[playernow].Setplayername(addname);

arr[playernow].Setplayerage(addage);

cout << "Already add this player!" << endl;

playernow += 1;

goto start;

}

if (answer == 4)

{

string n;

cout << "Enter the name of the player you wish to modify: ";

cin >> n;

int i, choice;

string new_name;

int new_age;

for (i = 0; i < playernow; i++)

{

if (arr[i].Getplayername() == n)

{

cout << "Which attribute you want to change?Press: 1.For changing name 2.For changing age ";

cin >> choice;

if (choice == 1)

{

cout << "Enter new name: ";

cin >> new_name;

arr[i].Setplayername(new_name);

goto start;

}

else

{

cout << "Enter new age: ";

cin >> new_age;

arr[i].Setplayerage(new_age);

goto start;

}

break;

}

}

if (i == playernow)

{

cout << "Player with name " << n << " do not exist ";

goto start;

}

}

if (answer == 5) // i am not sure with this one

{

string filename;

cout << "Enter the File name: ";

cin >> filename;

for (int i = 0; i < playernow; i++)

{

arr[i].save(filename);

}

goto start;

}

if (answer == 6)

{

// auto return to main

}

else

{

cout << "Use number 1-6" << endl;

goto start;

}

}

}

void menu2()

{

/// OPTION 2 in heree

}

int main()

{

int choice;

Player players[20];

int status = 1;

while (status == 1)

{

ask:

{

cout << "Press: 1.New File 2.Openfile 3.Quit ";

cin >> choice;

if (choice == 1)

{

menu1(players);

}

else if (choice == 2)

{

menu2();

}

else if (choice == 3)

{

status = 0;

}

else

{

cout << "Invalid number";

goto ask;

}

}

if (status == 0)

{

return 0;

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!