Need help to change an array to a vector

Dec 1, 2015 at 11:30pm
how can i change this array into a vector, this is what i had originally
1
2
3
4
5
6
7
8
9
10
11
void set_directions(words *dir)
{
	dir[NORTH].code = NORTH;
	dir[NORTH].word = "NORTH";
	dir[EAST].code = EAST;
	dir[EAST].word = "EAST";
	dir[SOUTH].code = SOUTH;
	dir[SOUTH].word = "SOUTH";
	dir[WEST].code = WEST;
	dir[WEST].word = "WEST";
}

and this is what I have done so far and it compiles but not as intended
1
2
3
4
5
6
7
8
  void set_directions(words *dir)
{
	vector<string> set_directions;  
	set_directions.push_back("North");
	set_directions.push_back("EAST");
	set_directions.push_back("SOUTH");
	set_directions.push_back("WEST");
}

any help is appreciated thanks in advance
Dec 1, 2015 at 11:36pm
and this is what I have done so far and it compiles but not as intended

What does "not as intended" mean? What do you want? How does this differ from what you want?
Dec 1, 2015 at 11:46pm
when i run the program it does not display the directions (north, south,east west). but maybe it is because i suppose i need to change something in a different function also,
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
void look_around(int loc, room *rms, words *dir, noun *nns)
{
	int i;
	cout << "I am in a " << 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;
		}
	}
}
the purpose of the game is for it to tell you in which room are you and when user imputs look it shows the exits and displays either north south east or west , then player imputs one of those directions and it sould take you to another rrom and ask again what user wants to do
Dec 2, 2015 at 12:12am
You are declaring the vector in set directions. Are you aware that you have created a local variable?
Dec 2, 2015 at 12:22am
no, i was not aware of that, so would moving that to main help?
Dec 2, 2015 at 12:24am
YES! Put it in main

Then pass it byreference. Do something like this

1
2
3
4
5
6
7
void set_directions(words *dir, vector<string> & set_directions)   // new parameter
{
	set_directions.push_back("North");
	set_directions.push_back("EAST");
	set_directions.push_back("SOUTH");
	set_directions.push_back("WEST");
}



Come to think of it, in fact, you probably want to replace "words *dir" altogether.
Last edited on Dec 2, 2015 at 12:25am
Dec 28, 2015 at 8:47am
Sorry Anthony,
I did not get your question on time. But Keannedawg has almost answer your question.
Once again sorry to answer you late.
Thanks.
Topic archived. No new replies allowed.