KingJohnno
KingJohnno

Reputation: 602

Creating a string with variables

I want to make an output string for my game. This involes getting an objects ID as well as the energy level. Is there a way to make this into a string

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

Thanks

JG

Edit: The return of getIdentifier() is a char.. Its sequencing, so A,B...Z

Upvotes: 0

Views: 113

Answers (2)

masoud
masoud

Reputation: 56479

Do not + the endl to a string. If you want a new-line, use '\n' instead.

#include <string>
using namespace std;

...

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

 

If the return type of getIdentifier() is a number, you can use std::to_string to convert it.

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

If it's a char you can use below way:

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

Upvotes: 4

Pendo826
Pendo826

Reputation: 1002

If you want an identifier to take in both a string and int pass it in through the function. You can say void

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

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

and then cout both of them.

I hope this helps.

Upvotes: 1

Related Questions