为什么我的令牌返回 NULL,我该如何修复它?(c++)
我创建了一个程序来获取用户输入的字符串并将其解析为令牌并根据输入移动机器人。该程序应该识别这些输入(其中x是整数):“前进x”“后退x”“左转x”“右转x”和“停止”。该程序对除“停止”之外的所有命令执行其应有的操作。当我输入“停止”时,程序会打印出“发生了什么?”因为我写了一行内容:
if(token == NULL)
{
cout << "whats happening?" << endl;
}
为什么 token 为 NULL,我该如何解决这个问题,以便它正确读取“停止”?
这是代码:
bool stopper = 0;
void Navigator::manualDrive()
{
VideoStream video(&myRobot, 0);//allows user to see what robot sees
video.startStream();
const int bufSize = 42;
char uinput[bufSize];
char delim[] = " ";
char *token;
while(stopper == 0)
{
cout << "Enter your directions below: " << endl;
cin.getline(uinput,bufSize);
Navigator::parseInstruction(uinput);
}
}
/* parseInstruction(char *c) -- parses cstring instructions received
* and moves robot accordingly
*/
void Navigator::parseInstruction(char * uinput)
{
char delim[] = " ";
char *token;
// cout << "Enter your directions below: " << endl;
// cin.getline (uinput, bufSize);
token=strtok(uinput, delim);
if(token == NULL)
{
cout << "whats happening?" << endl;
}
if(strcmp("forward", token) == 0)
{
int inches;
token = strtok(NULL, delim);
inches = atoi (token);
double value = fabs(0.0735 * fabs(inches) - 0.0550);
myRobot.forward(1, value);
}
else 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*/);
}
else 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.00467 * degrees - 0.04);
myRobot.turnLeft(1/*speed*/, value/*time*/);
}
else if(strcmp("right",token) == 0)
{
token = strtok(uinput, delim);
degrees = atoi (token);
double value = fabs(0.00467 * degrees - 0.04);
myRobot.turnRight(1/*speed*/, value/*time*/);
}
}
else if(strcmp("stop",token) == 0)
{
stopper = 1;
}
else
{
std::cerr << "Unknown command '" << token << "'\n";
}
}
/* autoDrive() -- reads in file from ifstream, parses
* and moves robot according to instructions in file
*/
void Navigator::autoDrive(string filename)
{
const int bufSize = 42;
char fLine[bufSize];
ifstream infile;
infile.open("autodrive.txt", fstream::in);
while (!infile.eof())
{
infile.getline(fLine, bufSize);
Navigator::parseInstruction(fLine);
}
infile.close();
}
我需要它来打破 while 循环并结束 manualDrive,因为在我的驱动程序中调用的下一个函数是 autoDrive。
autodrive.txt 文件如下所示:
forward 2
右转30
返回 3
左转50
stop
我还遗漏了我的程序的一个重要限制,我不允许使用标准库中的字符串
I've created a program to get a string input from a user and parse it into tokens and move a robot according to the input. The program is supposed to recognize these inputs(where x is an integer): "forward x" "back x" "turn left x" "turn right x" and "stop". The program does what it's supposed to for all commands except for "stop". When I type "stop" the program prints out "whats happening?" because I've written a line which states:
if(token == NULL)
{
cout << "whats happening?" << endl;
}
Why does token get NULL, and how can I fix this so it will read "stop" properly?
here is the code:
bool stopper = 0;
void Navigator::manualDrive()
{
VideoStream video(&myRobot, 0);//allows user to see what robot sees
video.startStream();
const int bufSize = 42;
char uinput[bufSize];
char delim[] = " ";
char *token;
while(stopper == 0)
{
cout << "Enter your directions below: " << endl;
cin.getline(uinput,bufSize);
Navigator::parseInstruction(uinput);
}
}
/* parseInstruction(char *c) -- parses cstring instructions received
* and moves robot accordingly
*/
void Navigator::parseInstruction(char * uinput)
{
char delim[] = " ";
char *token;
// cout << "Enter your directions below: " << endl;
// cin.getline (uinput, bufSize);
token=strtok(uinput, delim);
if(token == NULL)
{
cout << "whats happening?" << endl;
}
if(strcmp("forward", token) == 0)
{
int inches;
token = strtok(NULL, delim);
inches = atoi (token);
double value = fabs(0.0735 * fabs(inches) - 0.0550);
myRobot.forward(1, value);
}
else 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*/);
}
else 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.00467 * degrees - 0.04);
myRobot.turnLeft(1/*speed*/, value/*time*/);
}
else if(strcmp("right",token) == 0)
{
token = strtok(uinput, delim);
degrees = atoi (token);
double value = fabs(0.00467 * degrees - 0.04);
myRobot.turnRight(1/*speed*/, value/*time*/);
}
}
else if(strcmp("stop",token) == 0)
{
stopper = 1;
}
else
{
std::cerr << "Unknown command '" << token << "'\n";
}
}
/* autoDrive() -- reads in file from ifstream, parses
* and moves robot according to instructions in file
*/
void Navigator::autoDrive(string filename)
{
const int bufSize = 42;
char fLine[bufSize];
ifstream infile;
infile.open("autodrive.txt", fstream::in);
while (!infile.eof())
{
infile.getline(fLine, bufSize);
Navigator::parseInstruction(fLine);
}
infile.close();
}
I need this to break out of the while loop and end manualDrive because in my driver program the next function called is autoDrive.
the autodrive.txt file looks like:
forward 2
turn right 30
back 3
turn left 50
stop
Also I left out an important limitation on my program I'm not allowed to use string from the standard library
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
代码行:
如果
uinput
为空或仅由delim
字符串中的字符组成,则会将
token
设置为 NULL。稍微更改NULL
检查周围的代码可能会帮助您弄清楚发生了什么:在任何情况下,
NULL
都是strtok()
的正常返回code> 并且您的代码需要准备好处理它。The line of code:
will set
token
to NULL ifuinput
is empty or consists only of characters in thedelim
string.Changing the code around your
NULL
check a little might help you figure out what's going on:In any case,
NULL
is a normal return fromstrtok()
and your code needs to be prepared to handle it.尝试在调试器下运行并查看出现错误时 uinput 的值是什么。
还要检查此时的堆栈回溯:当您收到错误时,输入是来自manualDrive还是autoDrive?
Try running under a debugger and seeing what the value of uinput is when you get the error.
Also check the stack traceback at that point: when you get the error, is the input coming from manualDrive or autoDrive?