comparing strings & dodgy forever loop - help!

This is a discussion on comparing strings & dodgy forever loop - help! within the C++ Programming forums, part of the General Programming Boards category; Hi all Hoping someone can help me... I'm a C++ noob and I'm writing a simple "text adventure" (older members ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    43

    Question comparing strings & dodgy forever loop - help!

    Hi all

    Hoping someone can help me... I'm a C++ noob and I'm writing a simple "text adventure" (older members will know what that is!!) to help hone my skills (if that's what they can be called at this stage!).

    However I'm having two problems with the below member function:
    Code:
    void Office::user_input(door* pDoor, object* pObject, object* pFurniture, Room* pRoom) 
    {
    	string choice;
    	bool exit = false;
    	for ( ; ; ) 
    	{
    	cout << "What would you like to do? ";
    	cin >> choice;
    	
    	if (choice=="open door")
    		pDoor[0].open();
    	else
    		cout << "Not to worry, this is only a test.";
    	
    	if (exit==true)
    		break;
    	}
    }
    Firstly, the program is not recognising the link between choice and "open door" if I type in "open door" in the resulting console - it skips and goes straight to the else section. I have tried replacing the type of 'choice' from string to char* and using strcmp - however this didn't make any difference.

    Secondly, for some reason after typing in your command it is looping twice, ie printing "not to worry, this is only a test" and "what would you like to do?" twice before pausing to let the user type in their command again.

    I don't know if the two problems are related (although it seems doubtful). If anyone could shed any light it'd be really helpful. Cheers!

    J

  2. #2
    Always learning
    Join Date
    Apr 2009
    Posts
    25
    I've no idea why, but putting:
    Code:
    getline(cin, choice);
    instead of
    Code:
    cin >> choice;
    seems to work.

    Perhaps a more experienced programmer could enlighten us as to why. I have a feeling it's something obvious that will embarrass us :P

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    43
    well well, the two are linked after all. Nice work Mr Pointer!

    But I'm in total agreement... can anyone explain the why please?!! :-)

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    >> only reads until whitespace. When you type in "open door", it first reads "open". Then the loop restarts, finds that it still has "door" in the input buffer, gives you that, and doesn't even wait for input. So it takes two iterations to get to the input situation again, and you never get "open door".

    getline, on the other hand, reads until a newline, so it catches "open door" the first time around.

    Be careful when mixing >> and getline. >> leaves whitespace in the stream, including newlines, whereas getline doesn't. So if you do a >> and follow up with a getline, chances are that the getline will read the newline that was left in the stream and immediately return, without waiting for input.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    43
    Makes sense... many thanks!!

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    43

    Thumbs down

    doh... I'm now having another problem with this

    This is the updated code:
    Code:
    	string choice;
    	bool exit = false;
    	for ( ; ; ) 
    	{
    	cout << "What would you like to do? ";
    	getline (cin, choice);
    	
    	if (choice=="examine door" || "examine the door")
    		pDoor[0].examine(pObject);
    	else if (choice=="open door" || "open the door")
    		pDoor[0].open();
    	else
    		cout << "I don't understand the command you've entered.";
    	
    	if (exit==true)
    		break;
    	}
    It would now appear that regardless of what I type into the resulting console, the first option (to examine the door in this case) is always invoked.

    It would be a real shame if I ended up taking a sledgehammer to my laptop, so could anyone intervene before this happens?...

    J

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,980
    You need to compare choice with each of the string literals, e.g., (choice == "examine door" || choice == "examine the door"). At the moment what is happening is that even if choice is not equal to "examine door", "examine the door" will then be evaluated as true, hence the first option will always be selected.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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. comparing strings: case INsensitive
    By bchan90 in forum C Programming
    Replies: 7
    Last Post: 12-10-2008, 05:54 AM
  2. comparing strings.
    By goran00 in forum C Programming
    Replies: 3
    Last Post: 04-20-2008, 08:18 AM
  3. Comparing char array for two strings
    By Asbestos in forum C++ Programming
    Replies: 5
    Last Post: 02-15-2006, 11:41 PM
  4. comparing strings
    By gammacad in forum C Programming
    Replies: 3
    Last Post: 06-15-2002, 06:07 PM
  5. Comparing strings...
    By Nutshell in forum C Programming
    Replies: 8
    Last Post: 01-18-2002, 04:42 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21