User inputting multiple commands

I want to write a program that allows the user to enter commands like this:
cout << do what?
cin >> move this direction x spaces.

What is the best way of doing this? I have this program that works but I want to know if there is a better way? Thank you :)

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main()
{
    char move[5], steps[3];

    cout << "Move where?" << endl;
    cin.getline(move,5,' ');
    cin.getline(steps,3,'\n');
    cout << "\nYou moved " << move << " " << steps << " steps.";
    return 0;
}

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.
You can get the whole input line and parse it (looking for text to get the direction and for number to get the steps)
How do you search a string?
Thanks Mcleano, Bazzy, and Chris.
Sorry I should have mentioned std::string::find() :l
Sorry I should have mentioned std::string::find() :l

:P
Thanks again guys. Your posts were very helpful.
There is also the concept of a top-down parser. As simple as it can be:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <string>
using namespace 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;
  else if (dir == "south") y += n;
  else if (dir == "east" ) x -= n;
  else if (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";
      }
    else if (s == "get")
      {
      string item = get();
      cout << "You got a " << item << "!\n";
      }
    else if (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. ;-)

Good luck!
@Duoas ... you always have to do one better lol! *evils*
one better

More than one...
Topic archived. No new replies allowed.