Thread: New to c++, need some help.

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

    New to c++, need some help.

    Hi, I've been trying to make a very simple text game, but I can't get it to work, the program gets stuck in the while loop, simply repeating the cout"invalid command" line, and i cant for the life of me figure out why:


    Code:
    #include <iostream>
    #include <string>
    
    
    
    
    using namespace std;
    
    
    int main()
    
    
    {
    
    
    
    
    string option1;
    
    
    
    
    
    
    
    
    cout<<"What would you like to do?\n";
    cout<<"walk to door";
    cout<<"\nwalk to chest";
    getline(cin, option1, '\n');
    
    
    
    
     while (option1 != "walk to door" "walk to chest" ) {
         cout<<"Invalid command, please reenter:  \n";
         getline(cin, option1, '\n');
     }
    
    
    
    
     if (option1 == "walk to door") {
      cout<<"You walk towards the door";
     }
    
    
      else {
       cout<<"You walk towards the chest";
      }
    cin.ignore();
    }

    I greatly appreciate any help you can offer

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    The problem is that your while statement is malformed:
    Code:
    while (option1 != "walk to door" "walk to chest" )
    In C/C++ the condition statement can not be compounded, you must use the logical operators to combine two or more conditions. And each condition must have a complete comparison.
    Code:
    while (option1 != "walk to door" && option1 != "walk to chest" )
    Note: The above code may not be using the correct logical operator for your solution.

    Jim

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    3
    That's it!
    Thanks you so much Jim!

Popular pages Recent additions subscribe to a feed