### i have no idea what is wrong i am trying to create an interactice fiction world

I am trying to create an interactive fiction world using c++ and I can move north and south but not east and west. I have no idea what's wrong.

#include <iostream>

using namespace std;

int main ()
{
cout << "Please type a command.";
int x = 1;
int roomnumber = 000;
while (x == 1)
{
char input [10];
char west [] = "west";
char east [] = "east";
char north [] = "north";
char south [] = "south";
cin >> input;
if (strcmp (input, west) == 0)
{
roomnumber = roomnumber + 10;
}
if (strcmp (input, east) == 0)
{
roomnumber = roomnumber - 10;
}
if (strcmp (input, north) == 0)
{
roomnumber = roomnumber + 1;
}
if (strcmp (input, south) == 0)
{
roomnumber = roomnumber - 1;
}
if (roomnumber == 000)
{
cout << "You're in the entry hall.";
}
if (roomnumber == 010)
{
cout << "You're in the west.";
}
if (roomnumber == -010)
{
cout << "You're in the east.";
}
if (roomnumber == 001)
{
cout << "You're in the north.";
}
if (roomnumber == -001)
{
cout << "You're in the south.";
}
}
return 0;
}
In C++, if you start a number with a zero, it is taken to be a number in octal rather than decimal.

Lets me show you:
* * * * * * * *
This is 8 stars in decimal, or put another way, it is 010 stars in octal.

This being C++, we have proper C++ string objects that are much safer and easier. If you don't have a really good reason to use char arrays, use string objects instead.
Last edited on
Ok thanks!
Firstly I do not get the point of the code. The solution is change the 10 as a 2 which likes below;

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556`` ``````#include using namespace std; int main () { cout << "Please type a command."; int x = 1; int roomnumber = 000; while (x == 1) { char input [10]; char west [] = "west"; char east [] = "east"; char north [] = "north"; char south [] = "south"; cin >> input; if (strcmp (input, west) == 0) { roomnumber = roomnumber + 2; } if (strcmp (input, east) == 0) { roomnumber = roomnumber - 2; } if (strcmp (input, north) == 0) { roomnumber = roomnumber + 1; } if (strcmp (input, south) == 0) { roomnumber = roomnumber - 1; } if (roomnumber == 000) { cout << "You're in the entry hall."; } if (roomnumber == 2) { cout << "You're in the west."; } if (roomnumber == -2) { cout << "You're in the east."; } if (roomnumber == 001) { cout << "You're in the north."; } if (roomnumber == -001) { cout << "You're in the south."; } } return 0; }``````

I tried it and it was working.
Last edited on
Topic archived. No new replies allowed.