If the user chooses 1 of 3 objects, how can I get the rest of the program to us that object

I am making a pokemon based text game. I gave the user of picking from three pokemon at the beginning. If the user chooses, lets say, Charmander (an object), then I want the rest of the program to use this object.

The problem is I have 2 more objects(the other 2 starting pokemon), and every time I call a method from the class, I am having to say (if user chose 1, use the first object. if user chose 2, use the second object. if user chose 3, use the third object).

How can I get the rest of the program to just use the user chosen object?

Here is a piece of main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 cout << "Would you like:" << endl << "1) Bulbasour (A powerful grass type)" << endl << "2) Charmander (A ruthless fire type)" << endl << "3) Squirttle (A ferocious water type)" << endl << endl;


    cout << "Enter which Pokemon you want(1, 2, or 3): ";
    cin >> choice;

    if (choice == 1)
    {

        cout << endl << endl << "You chose Bulbasour!!  Great choice!  What would you like his name to be: ";
        cin >> name;
        Bulbasour.SetName(name);
        cout << endl << endl << endl << endl<< "You named your Bulbasour " << Bulbasour.GetName() <<"!  " << Bulbasour.GetName() << " is level " << Bulbasour.GetLevel() << ", has an attack of " << Bulbasour.GetAttack() << " and a health  of " << Bulbasour.GetHealth();
    }
    else if (choice == 2)
    {
        cout << endl << endl << "You chose Charmander!!  Great choice!  What would you like his name to be: ";
        cin >> name;
        Charmander.SetName(name);
        cout << endl << endl << endl << endl<< "You named your Charmander " << Charmander.GetName() <<"!  " << Charmander.GetName() << " is level " << Charmander.GetLevel() << ", has an attack of " << Charmander.GetAttack() << " and a health  of " << Charmander.GetHealth();
    }
    else if (choice == 3)
    {
        cout << endl << endl << "You chose Squirttle!!  Great choice!  What would you like his name to be: ";
        cin >> name;
        Squirttle.SetName(name);
        cout << endl << endl << endl << endl<< "You named your Squirttle " << Squirttle.GetName() <<"!  " << Squirttle.GetName() << " is level " << Squirttle.GetLevel() << ", has an attack of " << Squirttle.GetAttack() << " and a health  of " << Squirttle.GetHealth();
    }


skipping a few lines

1
2
3
4
5
6

cout << endl << endl << "Here you will see your health bars.  After each turn, the status of the health  bar will be updated." << endl << endl << endl;
    cout << " " << name << " Bulbasour                   Charmander" << endl << " " << Bulbasour.GetHealthBar() << "                   " << Charmander.GetHealthBar(); //this is the line I am struggling with.  I want to say "userchoice.GetHealthBar()"
    cout << "When your Pokemon gets attacked, his health goes down, like this." << endl << endl << endl;




Also, I am trying to be able to replace a character from a string in the constructor. How can I do this?(aka something like userchoice.replace.GetHealthBar(9,10,' ') I know that isn't it, but something like that>)
Last edited on
Create one object after the user has made a choice.
This is what inheritance and polymorphism is for.
Create an abstract base class of pokemon with the required interface and then create three derived classes from this base class.
Something a little simpler is to create an array of your pokemon:
1
2
3
4
5
6
7
// Classname arrayname[size];
pokemon startingPokemon[3];

// Assign each one the correct objects
pokemonList[0] = /* Bulbasour */;
pokemonList[1] = /* Charmander */;
pokemonList[2] = /* Squirttle */;


And then when the user selects 1-3, use that value as the index for your array:
1
2
3
4
5
// To access the pokemon at the index of choice
pokemonList[choice - 1].SetName(/* Name */);

// You would also be able to set it's health the same way
pokemonList[choice - 1].SetHealth(/* New Health */);


Another alternative is to create a new object that contains the person's pokemon. Let's say you have the first 150 of them, and you have them all stored in an array:
1
2
3
4
5
6
7
// Classname variablename;
pokemon usersPokemon;

// Assign it to the value of a base pokemon
usersPokemon = pokemonList[0]; // Bulbasaur
usersPokemon = pokemonList[3]; // Charmander
usersPokemon = pokemonList[6]; // Squirtle 


usersPokemon would then have all of the functions and members that any other pokemon would.
Topic archived. No new replies allowed.