I've written a program to get a string input from a user and parse it into tokens and move a robot according to the input. My problem is trying to issue more than one command. The code looks like:
void Navigator::manualDrive()
{
const int bufSize = 42;
char uinput[bufSize];
char delim[] = " ";
char *token;
while(true)
{
Navigator::parseInstruction(uinput);
}
}
/* parseInstruction(char *c) -- parses cstring instructions received
* and moves robot accordingly
*/
void Navigator::parseInstruction(char * c)
{
const int bufSize = 42;
char uinput[bufSize];
char delim[] = " ";
char *token;
cout << "Enter your directions below: \n";
cin.ignore();
cin.getline (uinput, bufSize);
token=strtok(uinput, delim);
if(strcmp("forward", token) == 0)
{
int inches;
token = strtok(NULL, delim);
inches = atoi (token);
Navigator::travel(inches);
}
if(strcmp("back",token) == 0)
{
int inches;
token = strtok(NULL, delim);
inches = atoi (token);
double value = fabs(0.0735 * fabs(inches) - 0.0550);
myRobot.backward(1/*speed*/, value/*time*/);
}
if(strcmp("turn",token) == 0)
{
int degrees;
token = strtok(NULL, delim);
if(strcmp("left",token) == 0)
{
token = strtok(uinput, delim);
degrees = atoi (token);
double value = fabs(0.0041 * degrees - 0.0523);
myRobot.turnLeft(1/*speed*/, value/*time*/);
}
}
if(strcmp("turn",token) == 0)
{
int degrees;
token = strtok(NULL, delim);
if(strcmp("right",token) == 0)
{
token = strtok(uinput, delim);
degrees = atoi (token);
double value = fabs(0.0041 * degrees - 0.0523);
myRobot.turnRight(1/*speed*/, value/*time*/);
}
}
if(strcmp("stop",token) == 0)
{
myRobot.motors(0,0);
}
}
In the function manualDrive I have a while loop calling the function parseInstruction infinitely. The program outputs "Enter your directions below: " When I give the program instructions it executes them, and then it outputs "enter your directions below: " again and when I input my directions again it does not execute them and outputs "Enter your directions below: " instead. I'm sure this is a very simple fix I'm just very new to c++. So if you could please help me out and tell me why the program only takes the first set of directions. thanks