2013-04-21 173 views
0

我想爲我的遊戲製作一個輸出字符串。這會獲取對象ID以及能量級別。有沒有一種方法,使之成爲一個字符串,這個用變量創建一個字符串

string Ouput = objects[x]->getIdentifier() + "Had the greater energy -> object" + objects[i]->getIdentifier() + "was deleted" + endl; 

感謝

JG

編輯:則getIdentifier的回報()是一個字符。它的排序,所以A,B ... Z

+1

什麼是字符串? – 2013-04-21 18:25:04

+2

使用'std :: string'。 – 2013-04-21 18:25:50

+0

我已經有了#include命名空間std;和#include 在頂部。這不夠嗎? – KingJohnno 2013-04-21 18:27:08

回答

4

不要+endl爲一個字符串。如果您需要換行,請改用'\n'

#include <string> 
using namespace std; 

... 

string Ouput = objects[x]->getIdentifier() + .... + "was deleted\n"; 
                   ^^ 

 

如果getIdentifier()返回類型是一個數字,你可以用std::to_string將其轉換。

string Ouput = to_string(objects[x]->getIdentifier()) + .... + "was deleted\n"; 
       ^^^^^^^^^ 

如果它是一個char您可以使用下面的方法:

string Ouput = string(1, objects[x]->getIdentifier()) + .... + "was deleted\n"; 
+0

謝謝:-)當我調試代碼時,只顯示標識符。 – KingJohnno 2013-04-21 18:34:52

+0

@KingJohnno請向我們展示一個說明您的問題的完整示例。請務必只包含重新生成確切問題的代碼,不要再提供。還包括示例輸入和輸出。 – 2013-04-21 18:39:18

+0

我收到錯誤「無法添加兩個指針」。 - 出現問題是因爲沒有任何內容輸出到屏幕上。 (理想情況下,我想寫這個文件作爲一個字符串) – KingJohnno 2013-04-21 18:45:02

1

如果你想要一個標識符采取兩個字符串和int在其穿過的功能。你可以說無效

void getIdentifier(int id, string description) 
{ 
cout << "What is the id\n"; 
cin >> id; 

cout << "What is the description\n"; 
cin >> description; 
} 

然後cout他們兩個。

我希望這會有所幫助。

+2

這是行不通的。您正在通過值來傳遞「description」。您需要通過引用它來傳遞它,您將其用作返回值。 'std :: string getIdentifier(int i)'可能會更好。 – 2013-04-21 18:38:40

相關問題