I am trying to make a simple game. The part I'm struggling with is giving the player the option of choosing to go a certain direction.
In my case it's something like this:
cout >> "Would you like to come with me to my house, stay here by your self, or go home with your friends?;
How do I make is so people can choose one of the paths by typing like "go with you" or "stay here" or "go with friends"
Please explain this as basic as you can.
Here's a piece of the game:
1 2 3 4
cout << name << " Would you like to come with me, go home with your friends, or stay here by yourself?";
if (stay here);
cin >> "stay here";
cout << "You stay where you're at.;
declare strings called stay and go with their corresponding responses in the game. then if the string inputted to cin is "stay here" the character will cout the appropriate response and vice versa
Here's what we have for the part: It's running fine but when I type "go with" in game it simply ignores everything and goes into the final 10000s sleep...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
cout << "((Type 'go with' to go with the old man or type 'stay' to stay where you are.))";
string choose;
cin >> choose;
if (choose == "go with")
{
cout << "You muster up your last bit of strength and begin following the Hecktor.";
}
elseif (choose == "stay")
{
cout << "You decide to stay where you are and continue resting.";
}
Sleep(100000);
}
It shouldn't be like that. I mean a string is an array of characters and a space is a character so it shouldn't throw it off. Try what I showed you and declare the answers as strings in their own right and the compare the string choose with go or stay. If that doesn't solve it then I guess just use simpler answers like "stay" or "go".
The problem is on line 2. Using cin with a string that way only gets the first word. You will need to use something like std::getline() to get an entire line.