Accessing data function/members?

On lines 5, 11, 42 there are issues. I have the incorrect code to tell the program which room i am in. Not sure for the proper code to use to get the correct results. Still very new to C++.


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
45
46
47
48
49
50
51
52
53
54
55
56
57
//--------------------------------------------------------------------------------------------
void look(int loc, vector<Room> &rms, vector<Words> &dir, vector<Noun> &nns)
{
    int i;
    cout << "I am in  "<< rms[loc]. description << "." << endl;
    
    for (i = 0; i <DIRS; i++)
    {
        if (rms[loc].exits_to_room[i] != NONE)
        {
            cout <<"There is an exit "<<dir[i].word <<" to a "
            <<rms [rms[loc].exits_to_room[i]].description << "." <<endl;
        }
    }
    
    //The look command should check which objects (nouns) are in the current
    //  room and report them to the player.
    for(i = 0; i <NOUNS; i++)
    {
        if(nns[i].location == loc)
        {
            cout << "I see " << nns[i].description << "." << endl;
        }
    }
}

//---------------------------------------------------------------------------------------------

bool parser(int &loc, string wd1, string wd2, vector<Words> &dir, vector<Words> &vbs,
            vector<Room> &rms, vector<Noun> &nns)
{
    static bool door_state = false; //false is a closed door.
    
    int i;
    for (i = 0; i < DIRS; i++)
    {
        if(wd1 == dir[i].word)
        {
            if(rms [loc].exits_to_room[dir[i].code] != NONE)
            {
                loc = rms[loc].exits_to_room[dir[i].code];
                cout << "I am now in a " << rms[i].description<< "." << endl;
              
                if(loc == FRONTYARD_1 || loc == HOUSE_1)
                {
                    nns [DOOR].location = loc;
                }
                //....
                return true;
            }
            else
            {
                cout << "No exit that way." <<endl;
                return true;
            }
        }
    }
Last edited on
What does your Room class look like? Maybe .description is a member function and not a data member, and you need to call it by using parenthesis ()
Here is the beginning of my code with my classes.

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <string>
#include <vector>
#include <cctype>

using namespace std;

enum en_DIRS {NORTH, EAST, SOUTH, WEST};
enum en_ROOMS {EASTSTREET, WESTSTREET, FRONTYARD_1, FRONTYARD_2, FRONTYARD_3, FRONTYARD_4,
 LIVINGROOM_1,LIVINGROOM_2,LIVINGROOM_3,LIVINGROOM_4, KITCHEN_1, KITCHEN_2, KITCHEN_3, 
 KITCHEN_4, HALLWAY_1, HALLWAY_2, HALLWAY_3, HALLWAY_4, RESTROOM_1, RESTROOM_2, 
 RESTROOM_3, RESTROOM_4, BEDROOM_1, BEDROOM_2, BEDROOM_3, BEDROOM_4, BEDROOM_5, 
 BEDROOM_6, BEDROOM_7, BEDROOM_8, HOUSE_1, HOUSE_2, HOUSE_3, HOUSE_4};
enum en_VERBS {GET, DROP, USE, OPEN, CLOSE, EXAMINE, INVENTORY, LOOK, YELL, SIT}; //2 more verbs

enum en_NOUNS {FRIDGE, CHAIR, TABLE, DOOR, MAILBOX, PIZZA, GARBAGECAN, WOMAN, MAN, KID, BIKE, CAR,BAT, NEWSPAPER, LAWNMOWER}; //make 15+ nouns
// New nouns = FRIDGE, CHAIR, TABLE, DOOR, MAILBOX, PIZZA, GARBAGECAN, WOMAN, MAN, KID, BIKE, CAR, 
// BAT, NEWSPAPER, LAWNMOWER
const int NONE= -1;
const int DIRS = 4;
const int ROOMS = 34;		// make 30 rooms
const int VERBS = 10;		// make 2 more verbs with appropriate actions
const int NOUNS = 15;

class Words
{
    
public:
    
    Words();
    
    string word;
	int code;
};

Words::Words()
{
}


class Room

{
private:
    int m_nSmelly;
public:
    
    
    
    int getSmelly();
    void setSmelly(int smelly);
    Room();
    ~Room();
    Room(string desc, int exitNORTH, int exitEAST, int exitSOUTH, int exitWEST);
    
	string description;
	int exits_to_room [DIRS];
};

Room::Room()
{
}
Room::~Room()
{
    
}
int Room::getSmelly()
{
    return m_nSmelly;
}
void Room::setSmelly(int smelly)
{
    m_nSmelly = smelly;
}

Room::Room(string desc, int exitNORTH, int exitEAST, int exitSOUTH, int exitWEST)
{
    description = desc;
    exits_to_room[NORTH] = exitNORTH;
    exits_to_room[EAST] = exitEAST;
    exits_to_room[SOUTH] = exitSOUTH;
    exits_to_room[WEST] = exitWEST;
}

class Noun
{
public:
    Noun();
	
    string word;
	string description;
	int code;
	int location;
	bool can_carry;
};
Noun::Noun()
{
}
What exactly is the problem? Is there a compilation error? Does it not output the correct info at runtime?
It does not output the correct info when run.
Can you give an example?
On line 42,
 
 cout << "I am now in a " << rms[i].description<< "." << endl;

This should return with the room I am in, as i go North, south, east or west.
 
 cout  rms[i].description

I must have the wrong code, because when the program is run, it returns with
"What shall i do?south
I am now in a.
What shall i do?"
Show the code that sets the descriptions.
Topic archived. No new replies allowed.