having trouble with while loops

I'm doing a text based adventure game and I'm trying to do it the simplest way possible.



cout << "Well then, its kind of dark in the brig, I need to get a better look around!. \n" << endl;
cin >> temporary;
while (temporary != "look" || temporary != "Look" || temporary != "LOOK")
{
cout << "I need to get me bearings before I can do anything, better take a look around. \n" << endl;
cin >> temporary;
}


i have a bunch of these while loops so that if the user enters something wrong it'll try to correct them and wait for them to enter it right. The problem is is that I tried running it, and it stuck me in the while loop even if I entered it right

PLEASE HELP!
Last edited on
I think you want to use && instead of ||.
|| is logical or. In the light of that, consider what happens when you enter, say, "Look".
the && worked, I guess I was just thinking "if its not Look or LOOK or look, then do this"
Ok, heres the next pot hole I'm about to run into cause I know I did it wrong.

while (temporary != "get key" && temporary != "Get key" && temporary != "Get Key" && temporary != "GET KEY")
{
cout << "Theres not much you can do right now while locked in the brig. But your Key's still on the floor!" << endl;
cin >> temporary;
}

What should I do for the two word response its looking for? I know I need to use getline but how do I implement it so it knows to have both words and then enter, and then it knows to look for those two words in that string?
Read the whole line
1
2
std:.string temporary;
std::getline(std::cin, temporary);


To avoid having to compare all kinds of upper case and lower case combinations you should make a function that compares two strings in a case insensitive way. If you want you can make it even smarter by ignoring spaces at the start and end of the string, and ignoring two spaces after each other.
getline reads up to the next newline, everything else follows from that.
getline(cin,temporary);


And instead of testing for a dozen different variants of each possible input, you can just transform the input to lower case and test for that (or use a function that compares for equality with no case sensitivity).

Edit: okay, too slow.
Last edited on
well the problem I have now is I have a cin n the lines earlier before a while loop checker.

When I ran it and typed in the correct "look" it went through that while loop, then ran right through the get line of the next input and into the while loop saying "You typed this in wrong."

Does my entire function need to be getline input based or did I just do something else wrong?
ok guys, I hate to answer my own questions. But adding getline to every input worked

Also I think you for the suggestion of adding lowercase checkers everywhere. But this thing si due in three days and I have no time to try and make and test to see if something like ta would work, then add it everywhere needed.

Also I would like to say again that It worked, 1 room down! 9 to go!

Thanks again guys and I hope to get more help from you in the future as my program progresses
I think the problem is when you read one word like this cin >> temporary; there is still a newline character in the input buffer so when you later call getline it will read the rest of the line which might be empty. I guess you could use cin.ignore but It is probably easier to just use getline all the time. If you want you can check if the line was empty and just ignore it.

EDIT: ok you got it.
Last edited on
Topic archived. No new replies allowed.