undeclared identifier problem

I've been trying to create a program that's like that old video game 'Zork', where you can move from room to room just typing n, s, e, w. The one I'm making takes place in my house. The problem is that I have no idea what I'm doing, so tryed using #define. At first I got tons of problems, but no I've limtited it to just one, the undeclared identifier. here's the code so far:
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
#include <iostream>
using namespace std;
int main ()
{
char n;
char w;

#define kitchen
	cout << "you are in the kitchen. ";
		cin >> n;
		switch (n)
			case 'n':
				familyroom;
#define familyroom
	cout << "You are in the family room. ";
		cin >> w;
		switch (w)
			case 's':
				kitchen;
				{
					 kitchen;
	int x;
std::cin >> x;
return 0;
}
}

It says that familyroom is an undeclared idetifier, but kitchen is just fine. Could somebody tell me what I'm doing wrong?
There are several problems with your approach.
* with "#define" you declare a macro
#define PI 3.1415
Whenever you use PI in your code (besides in a string) the preprocessor pastes in "3.1415".
So cout << PI; is essentially the same as cout << 3.1415;
The preprocessor only scans the line that contains the #define, in your case you simply define the tokes "kitchen" and "familyroom" but they contain nothing.

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
#include <iostream>
using namespace std;
int main ()
{
char n;
char w;

#define kitchen                 // your macro ends here
	cout << "you are in the kitchen. ";  // this line is not part of the macro but normal code!
		cin >> n;
		switch (n)
			case 'n':
				familyroom;   // this identifier is not known yet!
#define familyroom
	cout << "You are in the family room. ";
		cin >> w;
		switch (w)
			case 's':
				kitchen;   // after the preprocessor passed this line it will be ";", which is valid but not what you intended
				{
					 kitchen;
	int x;
std::cin >> x;
return 0;
}
}


You can #define spanning more lines by using a "\" as the last character of a line but that wouldn't be much of help in your case.

In Python for example "def" defines a function. In C++ #define defines a macro which is completely different!

* Your approach wouldn't work even if kitchen and familyroom we're functions since they never are called.
* your switch statement is also not correct.

Get yourself a good book or tutorial on defining functions.

FYI, a Zork project is perfect for a data-driven paradigm.

Essentially, if you do it right, your C++ program should be tiny and the rest should just be data.
By updating the data, you should be able to add new rooms, new responses, etc...

Since you are just learning C++, it's too early for this, but you should consider this type of architecture for V2 or V3.

Topic archived. No new replies allowed.