C++ String Tokenizer

AnnoraxAnnorax MA
edited July 2005 in Internet & Media
I am working on a text RPG and I need a way to parse the user's commands and I think tokenizing the input would be the easiest so I can piece together the bits. However, it is not working well for me. Here is the piece of my code so far:

cout << "> ";
cin >> command;
str = strtok(command, " ");

while (1)
{
/* extract string from string sequence */
str = strtok(NULL, " ");

/* check if there is nothing else to extract */
if (str == NULL)
{
cout << "Tokenizing complete\n";
exit(1);
}

/* print string after tokenized */
printf("%i: %s\n", x, str);
x++;
}

This is a test running from a tokenizer I got at metalshell.com and I want to get it to parse a user input, which it won't do. It runs and I input text, then it says "Toeknizing Complete" then it ends.

I am fairly new to C++ so I don't know how to fix it. And what do the %i and %s do?

Thanks for any help. :cool:

Comments

  • shwaipshwaip bluffin' with my muffin Icrontian
    edited July 2005
    I'm currently looking over the strtok docs - will try to let you know what's wrong - soon, hopefully.

    %i says put the second argument of printf here, and format it as an int
    %s says put the third here, format as a string.


    actually - before I try to fix it (heh), I guess I wonder what kind of commands you'll be taking in. simple characters or longer commands (I only ask because there may be an easier way to do it).
  • edited July 2005
    Annorax wrote:
    I am working on a text RPG and I need a way to parse the user's commands and I think tokenizing the input would be the easiest so I can piece together the bits. However, it is not working well for me. Here is the piece of my code so far:

    cout << "> ";
    cin >> command;
    str = strtok(command, " ");

    while (1)
    {
    /* extract string from string sequence */
    str = strtok(NULL, " ");

    /* check if there is nothing else to extract */
    if (str == NULL)
    {
    cout << "Tokenizing complete\n";
    exit(1);
    }

    /* print string after tokenized */
    printf("%i: %s\n", x, str);
    x++;
    }

    This is a test running from a tokenizer I got at metalshell.com and I want to get it to parse a user input, which it won't do. It runs and I input text, then it says "Toeknizing Complete" then it ends.

    I am fairly new to C++ so I don't know how to fix it. And what do the %i and %s do?

    Thanks for any help. :cool:

    Is this all the code you have? I would like to see the whole program.

    where did you declare command and str. Are they char arrays?

    You want to printf with x but I see no declaration or initialization.

    I use strtok all the time. It is pretty straight forward. Just know that you mangle the original char * when you call strtok the first time and again with each subsequent call.

    Also if you where testing with only one command you would not print anything. str would point to the next character after the first space. Then the test in the while would return NULL and the program would exit.

    CSwett
Sign In or Register to comment.