Keyboard recognition

Hello, I am working on a text based adventure game. The problem is how would I go about doing something such as when the player presses the character i would open up the inventory and show them what items they have?
how about:
1
2
3
4
5
6
7
8
char x;
cout<<"Enter the character:"<<endl;
cin>>x;
if (x=='a')
{
//open inventory
}
I'd do the same xkcd83 suggests, only with a string instead of a char since you probably want to incorporate this as an input option for the player. What I mean is something like:

cout<<"What do you want to do: "<<"\n";
string response;
cin>>response;
if (response == "i") {
cout<<"Here is the menu";
}
else if (response == "go_north") {
cout<<"Transferring you north";
}
else {
cout<<"I didn't understand you";
}
}

Instead of "Here is the menu" you'd probably have something like a function "Show_Menu" in there that prints the menu to whatever output you're using.

EDIT: You can't have blank lines in the player response if you do it this way though, that's why it's "go_north" instead of "go north". If you want the player to reply with more than one word you'd have to use something more complicated like that getline-function from the iostream-stream class.
Last edited on
Topic archived. No new replies allowed.