text rpg help

I've been working on the concept for a text rpg where one can move from room to room. Here's the code for the concept:
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
99
100
101
102
103
#include <iostream>

using namespace std;

bool north;
bool south;
bool east;
bool west;

void getlocationinfo(int xpos, int ypos, int zpos)
{ 
	if (xpos == 0 && ypos == 0 && zpos == 0)
	{
		cout << "You are at the origin. Have fun.\n";
		east = true;
	}
	
	if (xpos == 1 && ypos == 0 && zpos == 0)
	{
		cout << "You are 1 over from the origin. Have fun.\n";
		west = true;
	}
}

void getnewlocation(char direction)
{
	if (direction == 'n')
	{
		if (north == true)
		{
			ypos = ypos + 1;
		}

		else
		{
			cout << "Sorry, you can't go that way.";
		}
	}

	if (direction == 's')
	{
		if (south == true)
		{
			ypos = ypos - 1;
		}

		else
		{
			cout << "Sorry, you can't go that way.";
		}
	}

	if (direction == 'e')
	{
		if (east == true)
		{
			xpos = xpos + 1;
		}

		else
		{
			cout << "Sorry, you can't go that way.";
		}
	}

	if (direction == 'w')
	{
		if (west == true)
		{
			xpos = xpos - 1;
		}

		else
		{
			cout << "Sorry, you can't go that way.";
		}
	}
}

void getlocation(int xpos, int ypos, int zpos)
{
	while (1)
	{
		getlocationinfo(xpos, ypos, zpos);
		char direction;
		cin >> direction;
		getnewlocation(direction);
		north = false;
		south = false;
		east = false;
		west = false;
	}
}


int main()
{
	getlocation(xpos, ypos, zpos);
	int x;
	cin >> x;
	return 0;
}


I believe I understand what the problem is. Because of the fact that xpos, ypos, and zpos are only declared in getlocationinfo(int xpos, int ypos, int zpos), they can not be referenced in getnewlocation(char direction). Am I correct? How can these errors be solved?
Also, I'm new to C++. Just a warning.
Last edited on
You are quite correct. They are being declared with a function, and so only that function may access them. The easiest way for someone new the C++ is to declare them outside of all the functions, in the same area your booleans are declared. Not fantastic coding practice, but if you haven't learned much about headers yet, that will be your best bet to do that. You can change your code from
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
bool north;
bool south;
bool east;
bool west;

void getlocationinfo(int xpos, int ypos, int zpos)
{ 
	if (xpos == 0 && ypos == 0 && zpos == 0)
	{
		cout << "You are at the origin. Have fun.\n";
		east = true;
	}
	
	if (xpos == 1 && ypos == 0 && zpos == 0)
	{
		cout << "You are 1 over from the origin. Have fun.\n";
		west = true;
	}
}


toooooo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
bool north;
bool south;
bool east;
bool west;
int xpos,ypos,zpos;

void getlocationinfo(int xposIn, int yposIn, int zposIn)
{ 
	if (xposIn == 0 && yposIn == 0 && zposIn == 0)
	{
		cout << "You are at the origin. Have fun.\n";
		east = true;
	}
	
	if (xposIn == 1 && yposIn == 0 && zposIn == 0)
	{
		cout << "You are 1 over from the origin. Have fun.\n";
		west = true;
	}
}
Thank you so much for the help.
Topic archived. No new replies allowed.