hey guys, im working on a small text based RPG in C++. What im trying to do is have the user prompted with a question, like "Which direction would you like to travel", then the user enters West or w. What would be the best way to cade this? or can someone point me to some source code to look at.
yes im using cin, but i meant what is the best way to compare what they input to what i have in my code. What i want to do is have them enter a direction or a command and then have a IF statement like:
If (command != "west" || w)
cout << "Error unknown command;
else
cout << "you are traveling west";
But when i try this, and have their input saved in a variable like command, i keep getting an error C++ forbids the comparsion between a pointer and an integar.
char direction;
cout <<"Enter the direction that you want to travel: ";
cin >> direction;
if (direction == 'w')
cout <<"You are moving west.\n";
//and whatever other commands
else
cout <<"Invalid command.\n";
system ("pause");
For alternatives, read this: http://www.cplusplus.com/forum/beginner/1988/
Or you can use CodeBlocks and not worry about it, because it always pauses console applications after they terminate.
A char can be represented by a string but a string can not be represented by a char. In your example, you use single quotes to define a multi characer literal constant, you need to use double quotes. You also need to tell the compiler what to compare "west" against. So:
1 2 3 4 5 6 7 8 9 10 11
std::string direction; /*Change To std::string Datatype*/
char direction;
cout << "please enter a direction.";
cin >> direction;
if (direction == "w" || direction == "west") /*We Are Now Comparing Strings*/
cout << "you are traveling west";
else
cout << "Error, unknown command";