Thread: Need someone to explain, in simple language, what I'm not getting.

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    3

    Need someone to explain, in simple language, what I'm not getting.

    I'm very new to c++, just 2 days, so I don't really know any complex language as of yet, so i was hoping someone could explain to me why my code isn't working, it almost compiles, but with one "else without an if" error, but even without that chunk of code, there's alot of freezing and loops that don't seem to work properly.

    Code:
     #include <iostream>#include <string>
    
    
    using namespace std;
    
    
    int main()
    
    
    {
    string option1;
    string charname;
    int doorkey = 0;
    string option3;
    string option2;
    
    
    cout<<"Greetings adventurer, I am Lolterzard..\n";
    cout<<"What is your name?\n";
    cout<<"Enter character name:   \n\n";
    getline(cin, charname, '\n');
     cout<<"Hello, " << charname << "..\n";
     cout<<"I will be your guide throughout your time in the cave..\n";
     cout<<"I will not always be able to tell you what to do..\n";
     cout<<"But if you require assitance, I will give any advice I can..\n\n\n";
     cin.ignore();
    
    
    
    
     cout<<"Press enter to begin..\n";
     cin.ignore();
    
    
     cout<<"You awaken..";
     cin.ignore();
     cout<<"You look around..";
     cin.ignore();
     cout<<"You are in a small room, you can smell dampness..";
     cin.ignore();
     cout<<"You look around the room, you can vaguely make out a door on the oposite side..";
     cout<<"You notice a small chest in the corner of the room\n\n\n";
    
    
    
    
    do
    {
    
    
    cout<<"What would you like to do?\n";
    cout<<"walk to door";
    cout<<"\nwalk to chest\n\n";
    getline(cin, option1, '\n');
    
    
    
    
     while (option1 != "walk to door" && option1 != "walk to chest" ) {
         cout<<"Invalid command, please reenter:  \n";
         getline(cin, option1, '\n');
     }
    
    
    
    
     if (option1 == "walk to door") {
      cout<<"\nYou walk towards the door..\n";
      cin.ignore();
      cout<<"\n\nUpon reaching the door, you decide to..\n";
      cout<<"Examine door\n";
      cout<<"Open door\n\n";
      getline (cin, option2, '\n');
      }
    
    
      else {
       cout<<"\nYou walk towards the chest";
       cin.ignore();
       cout<<"You reach the chest, you think for a second, before deciding to..\n";
       cout<<"Examine chest\n";
       cout<<"Open chest\n\n";
       getline (cin, option2, '\n');
       }
          if (option2 == "examine door") {
          cout<<"The door is tall and wooden..\n";
          cout<<"You can smell a foul odour..\n\n";
          cout<<"How would you like to proceed?\n";
          cout<<"open door\n";
          cout<<"walk to chest\n";
          getline(cin, option2, '\n');
          }
    
    
          else if (option2 == "examine chest") {
           cout<<"The chest is quite small..\n";
           cout<<"It is made of wooden, with brass braces..\n\n";
           cout<<"You quickly make up your mind, and decide that you'll..\n";
           cout<<"open chest\n";
           cout<<"walk to door\n";
           getline (cin, option2, '\n');
          }
    
    
          else if (option2 == "open chest")
          cout<<"\n\nYou kneel down to open the chest..\n";
          cout<<"You open the chest, there's a key inside..\n";
          cout<<"Using your cat-like reflexes, you..\n";
          cout<<"Take the key\n\n";
    do {
          getline(cin, option3, '\n' );
    
    
          if (option3 == "take the key" && doorkey == 0) {
          cout<<"\nYou take a large metal key..\n";
          doorkey++;
          cin.ignore();
          cout<<"\nYou decide it's about time that you..\n";
          cout<<"walk to door";
          getline (cin, option3, '\n');
          }
    
    
          else {
          cout<<"invalid command, please reenter";
          getline (cin, option3, '\n');
        }
        } while (option3 != "take the key");
    
    
          else if (option2 != "open chest" && option2 != "open door") {
          cout<<"\nInvalid command, please reenter..\n";
          getline(cin, option1, '\n');
          }
    
    
          } while (doorkey <= 0);
    
    
    while (option2 != "open door")
      cout<<"\n\nYou walk to the door\n";
      cout<<"You think, before..\n";
      cout<<"open door";
      getline (cin, option2, '\n');
    
    
      cout<<"You open the door";
      cin.ignore();
    }
    Thanks for anyone who takes the time to read this.
    Sorry if bad English, not my first speak.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should indent your code properly. Always use braces for your if statements and loops.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Would you explain something to me about arrays in C language?
    By kelleannmccan in forum C Programming
    Replies: 6
    Last Post: 07-24-2011, 01:23 PM
  2. Simple Scripting Language Advice
    By timmeh in forum C++ Programming
    Replies: 3
    Last Post: 03-03-2010, 07:39 PM
  3. Explain this c language code....only 2 parts of it?
    By ankit8946 in forum C Programming
    Replies: 22
    Last Post: 10-31-2009, 03:33 PM
  4. Simple Phonebook using C language
    By almister in forum C Programming
    Replies: 7
    Last Post: 08-23-2009, 12:23 PM
  5. Simple query language
    By afflictedd2 in forum C++ Programming
    Replies: 3
    Last Post: 12-04-2008, 05:29 PM