2014-09-20 33 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

相關問題