-1

I'm fairly new to C++ programming, so this is probably a rookie mistake. BUT. (why can't I hide the rest of my code... I want to show ALL of what I'm doing..)

So first off, the start to my code looks like this (headers and such, plus the first few lines of code)

#include <iostream>
#include <string>

using namespace std;

string command;    
string playername; 
char response;     

string object;  

int main()
{

    do
    {          
       cout << "What is your name? "; 
       cout << endl;

       cin >> playername; 
       cout << endl;

       cout << "Your name is " << playername << "? (Y/N)" << endl; 
       cout << endl;
       cin >> response; 

       cout << endl;
       cout << endl;

    } while ((response != 'y')&&(response != 'Y')); 
}

And then here's the problem bit:

do
{  
    cout << endl;
    cout << "What will you do?";  
    cout << endl;

    cin >> command >> object; 

    if ((command == "look")&&(object == "door")) 
       cout << "You look at the door. It appears to be but a simple wooden door
       with a brass handle for opening and shutting. "; 

    else if ((command == "Open")&&(object == "door")) 
       cout << "You open the door and proceed to the next room";

    } while ((command == "look")&&(command == "Look")); 
}

My program will recognise when I put in "look" and "door" (as command and object) and responds with the appropriate "It's a door" line. BUT, when I put in "open" and "door" it just accepts it and ends the program, without displaying the text.

If there is ANY way I can rectify this I would be so very appreciative.

Thank you!

EDIT: for readability EDIT2: Changed last line to

}while ((command == "look")||(command == "Look")); 

and I get: the bulwarky that is this code

Marleen Schilt
  • 650
  • 15
  • 26

3 Answers3

2

The expression (command == "look")&&(command == "Look") will only be true if command is both "look" and "Look" at the same time, something which is impossible and will make the condition false.

You should use logical or instead: (command == "look")||(command == "Look")

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 1
    Also "Open" is only specified with a capital O. Might be worth noting. – Victor Sand Mar 30 '16 at 06:47
  • Oh goodness I know. I've tried all ways it seems. I've noted capital O and small O. I've tried the || to differentiate between everything. I'm just at a loss – KitKatarine Mar 30 '16 at 07:12
  • Or you can use the Boost library to make all your commands to non-caps characters. Then you don't need to compare your command with both "look" AND "Look", but you'll be fine with just comparing your command with "look". See http://stackoverflow.com/questions/3403844/tolower-function-for-c-strings – Marleen Schilt Mar 30 '16 at 07:26
  • Boost is gonna need more research on my part, because from that post I don't understand how it's supposed to work. Thank you, though – KitKatarine Mar 30 '16 at 07:48
  • The link I posted also mentions an STD library solution: std::transform(command.begin(), command.end(), command.begin(), ::tolower); – Marleen Schilt Mar 30 '16 at 07:56
0

Your problem depends on the differents between "Open" and "open". It is realy hard so check all strings for both cases. So maybe you could use the std::toupper function.

try something like this:

if(toupper(command) == toupper("open") && toupper(object) == toupper("door"))

Edit: Reference to the toupper-function

AndyC
  • 1
  • 1
0

Your read and print code will only work once because your condition is not valid. I assume that if you enter "Open" and "door" as your first commands you will get your open text, and then the program stops.

The reason your program will once read your code is because a do-while loop is build up that way. It will do your loop once, and then checks if it needs to be done again by seeing if the while(..) statement is still valid.


Here is some extra information on the do-while loop. http://www.tutorialspoint.com/cplusplus/cpp_do_while_loop.htm

Quoted from linked website:

The syntax of a do...while loop in C++ is:

do 
{ 
   statement(s);
}
while( condition ); 

Notice that the conditional expression appears at the end of the loop, so the statement(s) in the loop execute once before the condition is tested.


I'm not sure which behaviour you want in your program; when do you want to stop the program? If you want to continue looking and opening doors, I suggest you change your while(..) statement to

while (command != "stop");

This way the program will continue until you type in the word "stop"

Marleen Schilt
  • 650
  • 15
  • 26
  • I thought I had done it correctly. I want it to loop as long as the user inputs look, but not when the user inputs open. So far (and I'm sure I didn't explain this right in the main post), I can get it to loop when i type look, which is the expected behaviour, but when I enter open, it doesnt display the text that I want. and instead just terminates the program. – KitKatarine Mar 30 '16 at 07:46
  • Now it does this: https://i.gyazo.com/83930a7d4932f7b588069473f4417a11.png Ahh I'm bad at this... The behaviour I want is: Loop while user looks at the door. But I want it to show the text "You open the door and proceed.. blabla" when the user OPENS the door. Which it is currently not doing. I really appreciate your help, by the way.. – KitKatarine Mar 30 '16 at 07:52
  • Try typing "Open" with a capital O, since you're comparing it with "Open" in your else if(..) – Marleen Schilt Mar 30 '16 at 07:54
  • AHA! That got what I.. oh my gosh I feel silly. but now its looping when I open as well... Okay. I am gonna tinker with it some more. Thank you both!! – KitKatarine Mar 30 '16 at 07:55
  • I FIXED IT. Oh thank you so much. It's fixed. It works. – KitKatarine Mar 30 '16 at 07:58
  • No problem, glad it works now. When you look at it, it's really quite logical. Also try to get experience using a debugger, then you can really see what the program is doing. If you see that the value of command is "open", you can see that it's not the same as "Open". – Marleen Schilt Mar 30 '16 at 07:58