How to concatenate values of different types into a string?

Asked

Viewed 78 times

1

How do I concatenate all this string within a variable? Have some more elegant way to solve my problem?

int num1 = 1;
int num2 = 2;
int num3 = 3;
char simbol1 = '+';
char simbol2 = '-'; 

string equation = num1 + " " + simbol1 + " ( " + num3 + " " + simbol2 + " " + num3 + " ) = ? ";

1 answer

2


I imagine this is what you want:

auto equation = to_string(num1) + " " + simbol1 + " ( " + to_string(num2) + " " + simbol2 + " " + to_string(num2) + " ) = ? ";

I put in the Github for future reference.

You need to convert the number to string before concatenating.

  • Exactly that. Thank you.

  • @Markuswhite see the [tour] how to say thank you in the best way. You can vote on everything on the site.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.