Using sscanf in c ++

Hello i need help using sscanf to parse commands entered by the user.

The function sscanf() can be used to parse the command lines entered by the user. Suppose the user has entered a command at the keyboard and you have stored it in a character array called command. Suppose also that there are two character arrays called cmd1 and cmd2. To copy the first word out of command into cmd1 use this command:

sscanf(command, "%s", cmd1);

You can now test cmd1 using strcmp() to determine what the command is. If it is a command for which there is a second word (example: GO) then you can read both words using this command:

sscanf(command, "%s %s", cmd1, cmd2);

Now you can check cmd2 to determine the rest of the command which must then be executed. Note: You cannot assume that every command will have two words. The QUIT command is only one word. You will get an error if you try to read the one-word command using sscanf(command, "%s %s", cmd1, cmd2);

The first commands are GO, FIGHT, TAKE, QUIT
if the user enters GO the seconds words are NORTH, SOUTH, EAST, WEST, UP, DOWN

I really don't know where to start.. thanks

I think you should forget character array, sscanf and other old C string functions unless it's homework and you have to use it.
In C++ you can use a string and stringstream
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main()
{
  cout << "Enter command: ";
  string cmd;
  getline(cin, cmd);
  stringstream ss(cmd);
  string cmd1;
  if (ss >> cmd1)
    cout << "cmd1 = " << cmd1 << "\n";
  string cmd2;
  if (ss >> cmd2)
    cout << "cmd2 = " << cmd2 << "\n";
}

OUTPUT

Enter command: go east
cmd1 = go
cmd2 = east
While I agree with regard to avoiding scanf() or its variations, and using c++ features instead, there is a particular logic implied in the original question.

1. read cmd1 from command
2. test cmd1  has the value "GO"
    if yes, start again and read both cmd1 and cmd2 from command


In fact, the amount of detail given suggests that sscanf(), strcmp() and character arrays must be used - which is the C approach, but it may be necessary to satisfy requirements of the assignment.
Last edited on
@Chervil,
I am afraid you are right and he has to use sscanf(), strcmp() and character arrays
@Thomas1965 Maybe so. Though the logic which I highlighted could be done using C++ string/stringstream as well.
iosgaming700 wrote:
You will get an error if you try to read the one-word command using sscanf(command, "%s %s", cmd1, cmd2);

this isn't true, sscanf() returns the number of items found and scanned.

so
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
 
void processCommand(char *command)
{
  char cmd1[255];
  char cmd2[255];
  int numWords = sscanf(command,"%s %s",cmd1,cmd2);
  switch (numWords)
  {
   case 0:
      printf("no words found");
      break;
  case 1:
      printf ("found a single word '%s'",cmd1);
      // now search an array of [FIGHT, TAKE, QUIT] to see if its a valid single word
      break;
  case 2: 
      printf ("found two words '%s' and '%s'",cmd1, cmd2);

      // only GO supports a second word
      if (0!=strcmp("GO",cmd1))
     {
         printf("bad command, only GO has a second word");
     }
     else
     {
        // now search an array of [NORTH, SOUTH, EAST, WEST, UP, DOWN] to see if cmd2 is valid
     }
     break;
  }
}

Last edited on
yes i have to use sscanf in c++ for this assignment
Topic archived. No new replies allowed.