Oct 28, 2012 at 8:06pm UTC
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;
}
Oct 28, 2012 at 8:17pm UTC
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 Oct 28, 2012 at 8:20pm UTC
Oct 28, 2012 at 8:19pm UTC
Firstly I do not get the point of the code. The solution is change the 10 as a 2 which likes below;
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
#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 + 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 Oct 28, 2012 at 8:30pm UTC