0

I attempted to code a code a simple text adventure game from scratch as a programming exercise and got surprising results. Here is the complete code:

//Twisty Passages All Alike. //An adventure game designed as a practice exercise. #include <iostream> #include <string> using namespace std; int main() { cout << "\t\tTwisty Passages, All Alike!\n"; cout << "\tA text adventure by Jellypox Studios.\n"; //INVENTORY INDEX NUMBERS: //0. note //1. hammer //2. //ROOM NUMBERS: //0. living room //1. storeroom //Setting up the current room. int ALL_ITEMS = 10; string inventory[ALL_ITEMS]; int currentRoom = 0; string input = "DEFAULT"; //Room 0: living room. bool haveNote = false; bool haveHammer = false; //Room descriptions if (currentRoom == 0) { cout << "You are in the living room.\nThere are doors to the south and west and a staircase to the north.\nYou can see: a table, a cat, a fireplace.\n\n"; } else if (currentRoom == 1) { cout << "You are in the storeroom.\nThe room is a jumbled mass of cupboards, hanging sausages, left-out scraps of food and cockroaches.\nThe only thing of interest here is a large chest in the corner."; } //THE LIVING ROOM while (currentRoom == 0) { cin >> input; //THE TABLE if (input == "look table" || "look at table") { cout << "You see: some bones and scraps"; if (haveHammer = false) { cout << ", a hammer"; } if (haveNote == false) { cout << ", a note"; } cout << ".\n\n"; } else if (input == "look scraps" || "look at scraps" || "look bones" || "look at bones") { cout << "What a mess!\n\n"; } else if (input == "look note" || "look at note" || "read note") { if (haveNote == false) { cout << "Pick it up first!\n\n"; } else { cout << "It reads: \"Gone hunting. Will be back soon.\""; } } else if (input == "get note" || "take note" || "pick up note") { if (haveNote == false) { cout << "Got the note."; inventory[0] = "note"; haveNote = true; } else { cout << "You already have the note!\n\n"; } } else if (input == "look hammer" || "look at hammer") { cout << "It's just an ordinary hammer.\n\n"; } else if (input == "get hammer" || "take hammer" || "pick up hammer") { if (haveHammer == false) { cout << "Got the hammer.\n\n"; inventory[1] = "hammer"; haveHammer = true; } else { cout << "You already have the hammer!\n\n"; } } //ELSWHERE IN THE ROOM else if (input == "look cat" || "look at cat") { cout << "It stares up at you irritably.\n\n"; } else if (input == "look fireplace" || "look at fireplace") { cout << "A flickering fire warms the house.\n\n"; } else if (input == "south" || "go south") { //CODE TO MAKE THE DOOR UNLOCKABLE GOES HERE! cout << "The door is locked.\n\n"; } else if (input == "west" || "go west") { currentRoom = 1; } else if (input == "north" || "go north" || "upstairs" || "go upstairs") { currentRoom = 2; } else { cout << "Say what?"; } } return 0; } 

I tried using a while loop for the input/output system so that if you type "look at table," it gives you a description of the table, etc. but instead, it does this:

look at table You see: some bones and scraps, a note. You see: some bones and scraps, a note. You see: some bones and scraps, a note. get note You see: some bones and scraps, a note. You see: some bones and scraps, a note. get hammer You see: some bones and scraps, a note. You see: some bones and scraps, a note. go west You see: some bones and scraps, a note. You see: some bones and scraps, a note. 

What all did I get wrong?

11
  • 3
    Welcome to Stack Overflow! Asking people to spot errors in your code is not especially productive. You should use the debugger (or add print statements) to isolate the problem, by tracing the progress of your program, and comparing it to what you expect to happen. As soon as the two diverge, then you've found your problem. (And then if necessary, you should construct a minimal test-case.) Commented Aug 14, 2013 at 22:27
  • 1
    It is pitch black. You are likely to be eaten by a grue. Commented Aug 14, 2013 at 22:28
  • 2
    Is the problem this line: if (haveHammer = false) Commented Aug 14, 2013 at 22:30
  • @MikeChristensen Sorry, I didn't see your comment at first. That doesn't seem to be the cause of my main problem, but thanks for pointing out that error! Commented Aug 14, 2013 at 22:34
  • 1
    @BenVoigt: A decent compiler will warn about assignment where a condition is usually expected, even if the type is boolean. Commented Aug 14, 2013 at 22:47

2 Answers 2

1

there are typos in this code, for example if (haveHammer = false)

which should have double `=' I believe. The compile won't yell at you but the logic is not what you want.

there are non C++ codes in this code,

 if (input == "hall..." || "ass" ) 

In C++, we don't use that although it is valid. It should be

 if (input =="hall....") || input == "bbbb") 

Since I think you won't just want to compare the memory address of these string literals.

(I did not read the code carefully, thanks for the comments.

Sign up to request clarification or add additional context in comments.

2 Comments

input is a std::string, so we certainly do do that in C++, although if we want it to work we do if ( (input == "hall") || (input == "ass") ).
In C++ we don't use strcmp .. In soviet russia strcmp uses C++
0

It prints "You see: some bones..." once for every word in your input. If you type "the quick brown fox jumped over the lazy dogs" it will probably print "You see: ..." 9 times. So it looks like your "cin >> input" is breaking up your input string into tokens. You have to figure out how to get it to read a whole line and give it to you as one string; I'm so rusty at C++ that I can't remember. :-)

Also, you wrote: if (input == "look table" || "look at table")

The problem here is that "look at table" is always true, so the expression is always true. You probably want: if (input == "look table" || input == "look at table")

2 Comments

Thank you, this clears everything up! (Except how to get it to stop splitting up my strings, of course. I'll just have to try Google. :)
Look at getline to process lines at a time.