2014-09-20 35 views
0

我在Xcode中引入了一个程序,但它在Visual Studio Express 2013中编译得很好。 这实际上是一个例子,学校打出来并用于向我们展示令牌如何在C++中工作。我没有写这个。我得到的错误是在以下几行:无法在Xcode中编译C++程序,但它在Visual Studio Express 2013中编译正常

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"; 
} 

回答

0

的Xcode不会让你在的地方空指针的使用 '\ 0'。从技术上讲,它应该让你这样做,并悄悄投到NULL。然而,为便于阅读,你真的应该通过NULL为空指针,就像这样:

while (tokenptr != NULL) 

,甚至完全跳过检查,像这样:

while (tokenptr) 

第二种方法来检查NULL是地道到C/C++,但不是每个人都喜欢它,宁愿添加!= NULL

同样地,你应该通过NULLstrtok

strtok(NULL, " ,.!?;:") 

最后,由于strtok不重入,可以考虑使用strtok_r代替。

+0

谢谢。我遇到的另一个问题发生了很多。 现在程序编译好了,但我实际上并没有输入框输入我的句子。我所做的是取代那个盒子,是一个带有黑色背景的盒子(类似于输入框),带有绿色字母(lldb)。我相信这与调试器有关。在左边是一个显示带有tokenptr变量的发送数组的框。发送char [50] tokenptr char *“” 左边有一个绿色的L,我可以扩展每个。 非常感谢! – mercuryrsng 2014-09-20 18:02:23

+0

@JustinPosner你应该问一个新的问题,以获得一些可能经历过同样问题的人的注意。 – dasblinkenlight 2014-09-20 18:41:12

相关问题