0

ゲームの出力文字列を作成したいと考えています。これには、オブジェクト ID とエネルギー レベルの取得が含まれます。これを文字列にする方法はありますか

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

ありがとう

JG

編集: getIdentifier() の戻り値は char です..その順序付けなので、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

識別子に string と int の両方を取り込ませたい場合は、関数を介して渡します。あなたは無効と言うことができます

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

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

そして、それらの両方を数えます。

これが役立つことを願っています。

于 2013-04-21T18:36:51.457 に答える