I’m coding a text-based RPG and I have my controls working: I have a string checking what you write in against an array of if
statements.
It can get it right with everything other than my pick up action.
My if statements are set out as follows:
int game_main() {
string PlayerOption;
//I have a few lines of cout statements just for my gui/hud
cin >> PlayerOption;
if (PlayerOption == "look") {
//it’s then just calling a script from a separate header file
}
else if (PlayerOption == "pick up") {
//another script from that header
}
My look command and all my others work but I don’t know how to set it up right.
I've tried putting it like "pick up"
, I tried it like "pick"" ""up"
and a couple other ways but I don’t know how to properly write the expression.
Even then my problem might be with my string and not my if statement so any suggestions would be greatly appreciated.
And I think @DMGregory might be right about the word boundaries because even with that command if I make it accept "pick" it works it’s only when I ask it for two words it really breaks
Is there any way I could get around that?
PlayerOption
is holding a two word value "pick up" and somehow that is comparing as not equal to the literal string "pick up"? Strings have no built-in concept of word boundaries, so this might point to a problem in how you populate your variable (maybe your input routine grabbed only "pick"?), differences in case, or a non-printing character in one of the strings. Or do you meanPlayerOption
contains an unknown word, like "pickup octopus" which naturally is not equal to "pickup"? \$\endgroup\$ – DMGregory♦ Nov 10 at 13:50cin
(standard input stream) and comparing it withif
statements. Tj, can you please clarify, does the problem occur when the player types in literally "pick up" or only when they try to add an object to the phrase, like "pick up unicycle"? \$\endgroup\$ – DMGregory♦ Nov 10 at 22:59