1

我创建了一个程序来从用户那里获取字符串输入并将其解析为令牌并根据输入移动机器人。该程序应该识别这些输入(其中 x 是一个整数):“前进 x”“后退 x”“左转 x”“右转 x”和“停止”。该程序对除“停止”之外的所有命令执行应有的操作。当我键入“停止”时,程序会打印出“发生了什么?” 因为我写了一行内容:

if(token == NULL)
{
    cout << "whats happening?" << endl;
}  

为什么令牌会变为 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 文件如下所示:

前进 2
右转 30
后退 3
左转 50
停止

我也遗漏了我的程序的一个重要限制,我不允许使用标准库中的字符串

4

2 回答 2

2

代码行:

token=strtok(uinput, delim);

如果为空或仅包含字符串中的字符,将设置token为 NULL 。uinputdelim

稍微更改一下NULL支票周围的代码可能会帮助您弄清楚发生了什么:

std::string original_uinput( uinput);  // save input string for debugging

token=strtok(uinput, delim);
if(token == NULL)
{
    cout << "whats happening? uinput was: " << original_uinput << endl;
}

无论如何,NULL这是一个正常的返回strtok(),你的代码需要准备好处理它。

于 2010-04-14T01:21:07.027 回答
0

尝试在调试器下运行并查看错误时 uinput 的值。

还要检查此时的堆栈回溯:当您收到错误时,输入是来自手动驱动还是自动驱动?

于 2010-04-14T00:59:42.390 回答