不能在XCODE中编译C++程序,但它编译VisualStudio Express 2013中的Frime

不能在XCODE中编译C++程序,但它编译VisualStudio Express 2013中的Frime,c++,xcode,strtok,C++,Xcode,Strtok,我在Xcode中遇到一个程序问题,但它在Visual Studio Express 2013中编译得很好。 这实际上是我的学校教授输入的一个例子,它用来告诉我们如何在C++中使用令牌。这不是我写的。我得到的错误如下所示: while (tokenptr != '\0') // while tokenptr is not a null (then must be a token) error is: "Comparison between pointer and integer('c

我在Xcode中遇到一个程序问题,但它在Visual Studio Express 2013中编译得很好。 这实际上是我的学校教授输入的一个例子,它用来告诉我们如何在C++中使用令牌。这不是我写的。我得到的错误如下所示:

 while (tokenptr != '\0')   // while tokenptr is not a null (then must be a token)
    error is: "Comparison between pointer and integer('char' and 'int)"

 tokenptr = strtok('\0', " ,.!?;:");  // get next token from null where left off
    error is: "No matching function for call to 'strtok'"
我觉得问题出在“\0”上,因为程序正在突出显示它。 谁能帮我理解这个问题吗

谢谢

/*
Exercise 3-12
 This program will change an English phrase to Pig Latin
 */
#include<iostream>
#include<cstring>
using namespace std;

void plw(char []);

int main()
{
    char sent[50], *tokenptr;
    cout << "Enter a sentence:  ";
    cin.getline(sent, sizeof(sent), '\n');  // input up to size of cstring or until enter
    tokenptr = strtok(sent, " ,.!?;:");  // get first token
    while (tokenptr != '\0')   // while tokenptr is not a null (then must be a token)
    {
        plw(tokenptr);   // convert this cstring token to Pig Latin
        cout << " ";   // put space back in (old space was delimiter and removed)
        tokenptr = strtok('\0', " ,.!?;:");  // get next token from null where left off
    }
    cout << endl;
}

// function to take each word token (or cstring or char array) and print as Pig Latin
void plw(char word[])
{
    int x;
    for (x = 1; x < strlen(word); x++)
        cout << word[x];
    cout << word[0] << "ay";
}

Xcode不允许您使用“\0”代替空指针。从技术上讲,它应该允许您这样做,并悄悄地将其转换为NULL。然而,为了可读性,您确实应该为NULL指针传递NULL,如下所示:

while (tokenptr != NULL)
while (tokenptr)
或者干脆跳过支票,就像这样:

while (tokenptr != NULL)
while (tokenptr)
第二种检查NULL的方法是C/C++惯用的方法,但不是每个人都喜欢它,更喜欢添加!=反正是空的

同样,您应该将NULL传递给strtok:


最后,由于Srtok不可重入,请考虑使用。

谢谢。我遇到的另一个问题经常发生。现在这个程序编译得很好,但我实际上没有输入框来输入我的句子。我得到的是一个黑色背景的框,类似于输入框中的绿色字母,表示lldb。我相信这与调试器有关。在它的左边是一个框,它显示了我发送的带有tokenptr变量的数组。sent char[50]tokenptr char*每个标记的左边都有一个绿色的L,我可以展开每个标记。非常感谢@贾斯汀波斯纳:你应该为此提出一个新问题,以引起其他可能经历过同样问题的人的注意。