If Statements, in If Statements.

I'm working on a simple text based adventure. I curently have the game tell you the coordinates you're at, then it checks a long list of if statements for what room you're in. In each of those statements I'll need another 2, or more if statements to check for actions, and movement. I currently only have the first room done. It says I'm missing "expected Primary functions" when I compile it though. If someone could help sort it out, I'd be very pleased.

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

int axisx;
int axisy;
axisy = 0;
axisx= 0;
char direction [6];
char action [10];

cout << "####################################################";
cout << "\n";
cout << "#                                                  #";
cout << "\n";
cout << "#              =Text Based Adventure=              #";
cout << "\n";
cout << "#                                                  #";
cout << "\n";
cout << "####################################################";
cout << "\n";
cout << "\n";

            

cout << "You're at the coordinates "; cout << axisx; cout << ","; cout << axisy; cout << ".";




{
		if(axisx == 0, axisy == 0)
			cout << "\nYou're in a room. You can go east, and north. \nThere's a table next to you.\n";
			cout << "What do you want to do?\nI want to ";
			cin >> action;
			{ if (action == "move");
			cout << "Where do you want to move?\n";
			cin >> direction;
        }
                { if (direction == "north")
                axisx+=1;
                else if (direction == "east")
                axisy+=1;
                }
                  
		else if(axisx == 0, axisy == 1)
			cout << "You're in a room. You can go east and south. \n There's nothing in here.\n";
		else if(axisx == 1, axisy == 0)
		    cout << "You're in a room. You can go west and north. \n There's a plant in the corner.\n";
		else if(axisx == 1, axisy == 1)
		    cout << "You're in a room. You can go south, east, and west. \n There's a corridor to your right.\n";
		else if(axisx == 2, axisy == 1)
			cout << "You're in a hallway. You can go east, and west. \n The light above you is flickering.\n";
	}	



cin.get();
cin.get();
return 0;

}
You're writing your if statements incorrect. The braces follow after the if statement
1
2
3
if ( axisx == 0 )
{
}


Also, your if statement conditions are not coded correctly. You must use the logical && (and) or logical || (or).

I would start by reviewing this:
http://www.cplusplus.com/doc/tutorial/control/
http://www.cplusplus.com/doc/tutorial/operators/
Thanks. Hopefully I can make myself a quick template that I can copy and paste for each one. Will make life so much easier.
I've got down the && function down, but my syntax for the if statements must still be off.
Here's what I've changed the first statement too.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
if (axisx == 0 && axisy == 0 )
{
          cout << "There's nothing around you. You can go north, and east.";
          cout << "What are you going to do?\nI'm going to ";
          cin >> action;
          if (action == "move" )
          {
                     cout << "Where would you like to move?";
                     cin >> direction;
                     }
                     
                     if (direction == "north" )
                     {axisy+=1;
                     
                     }
                     else if (direction == "east" )
                     axisx+=1;
                     
                     else
                     {cout << "Invalid input";
                     }
                     }
I'm unsure of what you're trying to do. I'm not sure of your indenting style, but I've tried to move things around a little so the individual blocks are more clear.

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
if (axisx == 0 && axisy == 0 )
{
    cout << "There's nothing around you. You can go north, and east.";
    cout << "What are you going to do?\nI'm going to ";
    cin >> action;
 
    if (action == "move" )
    {
        cout << "Where would you like to move?";
        cin >> direction;
    }
                     
    if (direction == "north" )
    {
        axisy+=1;
    }

    else if (direction == "east" )
        axisx+=1;
                     
    else
    {
        cout << "Invalid input";
    }
}


I think what you want to do is nest the third "if" inside the second? I'm not sure...
Last edited on
*sigh* Well, I thank you for the re-organization of my coding, but it seems to have done nothing. I believe what is wrong, is the char action isn't being changed.
I didn't mean the re-organization to actually help directly; I'm quite a new programmer myself -- merely I was hoping it would help others to help you.

What are the symptoms of your code? If you tell me what needs fixing, maybe I can help.

Is action a char? It should be a string.

Are you getting compile-time or runtime errors; and what are they?
Yes, action is a char, so is my direction code.
No compiling errors.
I didn't know to use strings.
I shall change that right away then.
Thank you for finally clearing this up.
Oh ok. No problem =]
Topic archived. No new replies allowed.