You could search the string for the word "move" then read the direction, then read the amount of spaces.
Of course, that relies on the user writing it in the correct order.
#include <iostream>
#include <string>
usingnamespace std;
void move( int& x, int& y )
{
string dir;
cin >> dir;
int n;
cin >> n;
if (!cin)
{
cin.clear();
cin.ignore( 1000, '\n' );
dir = "foo";
}
if (dir == "north") y -= n;
elseif (dir == "south") y += n;
elseif (dir == "east" ) x -= n;
elseif (dir == "west" ) x += n;
else cout << "What?\n";
}
string get()
{
string result;
cin >> result;
return result;
}
int main()
{
cout << "Commands are:\n"" move DIR N -- where DIR is one of [north, south, east, west] and N is\n"" the number of steps to take in that direction.\n"" get ITEM -- where ITEM is the object you wish to pick up.\n"" quit -- stop playing.\n\n";
int x = 0;
int y = 0;
while (true)
{
cout << "> " << flush;
string s;
cin >> s;
if (s == "move")
{
move( x, y );
cout << "You are now at location (" << x << "," << y << ").\n";
}
elseif (s == "get")
{
string item = get();
cout << "You got a " << item << "!\n";
}
elseif (s == "quit")
{
cout << "Good bye.\n";
break;
}
else
{
cout << "What?\n";
}
}
}
You will need to add some better error handling and the like (such as case-insensitive matching and abbreviations for input words). I also suggest that you get input with getline() and then parse the string using an istringstream. You will also want to put limits on what can be done (such as whether or not you can walk in a specific direction and whether or not a requested item can be picked up, etc). But those are exercises for you. ;-)