Help with classes!

Hi, I am currently learning C++ and thought it would be a good idea to join a forum as you guys can probably explain things a lot better than a book can!

So straight to the point, I decided to make a simple linear text based game to practice all of the basic features I'm learning but cant seem to find a way around one mechanic.

My game involves simply rolling a random number, and that number relates to a certain game event/location, for example "you have been ambushed and lose 2 hp", some may give hp, but you continue to roll until you are dead.

I figured the best way to manage locations would be with a class for them, So i could give them a description and show if they had been visited already with simple code

1
2
3
4
5
6
7
8
9
10
11
12
13
14

class location
{
      public:
      bool searched;
      string description;
      };
      
int main()
{
    location hospital;
    hospital.searched = false;
    hospital.description = (" a run down hospital \n");
}


Now the problem arises when I try to refer to a location randomly, I have tried creating a string called "currentlocation" and giving it the value "hospital" when its random number is called, but if i write

 
cout << currentlocation.description;


it will not work, it does not use the value of the string "hospital" as an identifier.

Is there any way to get around this?

PS. I apologise if I am missing something obvious but I have only been learning for a few months! thanks!
There easiest way I can think of for you (considering you have only been learning for a few months) is to use some kind of enumerator to keep track of what location you are in. Here is a quick example:
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
enum ELocations {
    HOSPITAL,     // = 0
    BACK_STREET,  // = 1
    // etc...
};

// ...

int main() {
    srand(time(0));   // Assuming you are using cstdlib for your random numbers
    bool finished = false;

    ELocations locationType;  // To remember where you are

    location hospital;
    hospital.searched = false;
    hospital.description = ("A run down hospital.\n");

    location backStreet;
    backStreet.searched = false;
    backStreet.description = ("A street behind the hospital.\n");

    while (!finished) {      // Main game loop
        switch (rand() % 6) {   // 6 possible locations
            case HOSPITAL: {
                cout << hospital.description;
                locationType = HOSPITAL;
                // Do stuff for a hospital
            } break;

            case BACK_STREET: {
                 cout << backStreet.description;
                 locationType = BACKSTREET;
                 // Do stuff for a back street
            } break;

            // etc.
        }

        // Keep going
    }

    return 0;
}


This works, though there are better ways, though you probably haven't learned them yet.
Last edited on
Thanks for the help, this way suits my level just fine!
Topic archived. No new replies allowed.