How do I get the next token in a Cstring if I want to use it as an int? (c++)
- by Van
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.