0

我想为我的游戏制作一个输出字符串。这涉及获取对象 ID 以及能级。有没有办法把它变成一个字符串

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

谢谢

JG

编辑: getIdentifier() 的返回是一个字符。它的排序,所以 A,B...Z

4

2 回答 2

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";
于 2013-04-21T18:31:56.660 回答
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 他们两个。

我希望这有帮助。

于 2013-04-21T18:36:51.457 回答