views:

270

answers:

4

My objective is to take directions from a user and eventually a text file to move a robot. The catch is that I must use Cstrings(such as char word[];) rather than the std::string and tokenize them for use.

the code looks like this:

void Navigator::manualDrive()
{

    char uinput[1];
    char delim[] = " ";
    char *token;

    cout << "Enter your directions below: \n";
    cin.ignore();
    cin.getline (uinput, 256);

    token=strtok(uinput, delim);

    if(token == "forward")
    {
        int inches;
        inches=token+1;
        travel(inches);
    }
}

I've never used Cstrings I've never tokenized anything before, and I don't know how to write this. Our T.A.'s expect us to google and find all the answers because they are aware we've never been taught these methods. Everyone in my lab is having much more trouble than usual.

I don't know the code to write but I know what I want my program to do.
I want it to execute like this:

1) Ask for directions.
2) cin.getline the users input
3) tokenize the inputed string
4) if the first word token == "forward" move to the next token and find out how many inches to move forward then move forward
5) else if the first token == "turn" move to the next token. if the next token == "left" move to the next token and find out how many degrees to turn left

I will have to do this for forward x, backward x, turn left x, turn right x, and stop(where x is in inches or degrees). I already wrote functions that tell the robot how to move forward an inch and turn in degrees. I just need to know how to convert the inputted strings to all lowercase letters and move from token to token and convert or extract the numbers from the string to use them as integers.

If all is not clear you can read my lab write up at this link: http://www.cs.utk.edu/~cs102/robot_labs/Lab9.html
If anything is unclear please let me know, and I will clarify as best I can.

A: 
  1. To convert to lower, you can use tolower. It operates one character at a time, so you need a simple loop.
  2. To parse a string into an integer, you can use strtoll.
  3. Move to the next token just means call strtok again (in this case inside an if statement).
Matthew Flaschen
A: 

To convert a string to lower case, you can use tolower(). tolower works on single chars, so you have to loop through your string and apply it to every char.

To convert a string to a number, use strtol, strtoul, or other related functions.

JSBangs
A: 

you simply call strtok again to get the next token.

You will probably need valid input checks i.e. check that there is a next value

typically you would use a while loop

ex

token=strtok(uinput, delim);
while (token != NULL)
{
   // use token

  //go to next token
   token=strtok(uinput, delim);
}
Holograham
A: 

While i can't say something about CString, I spotted a stack overflow fault in your code:

{
    char uinput[1]; // <- Here you define a one byte large buffer on the stack 
    char delim[] = " ";
    char *token;

    cout << "Enter your directions below: \n";
    cin.ignore();
    cin.getline(uinput, 256); // and here you put up to 256 bytes into it

I would sugest:

{
    const int bufSize = 42; // since 42 is the awnser to everything
    char uinput[bufSize];
    // ...
    cin.getline(uinput, bufSize);
Rudi
thank you for the suggestion I will certainly do so.
Van
and I luled at the The Hitchhiker's Guide to the Galaxy allusion.
Van