What is the “right way” to do the following? (Note: I do not want to display a message on the screen (yet), the data should be stored in a variable.)
std::cout << "Enter a letter: ";
char input;
std::cin >> input;
std::string message = "Today program was brought to you by the letter '" + input + "'.";
The code gives me an error message with invalid type operands const char*and const char [3]binary operator+.
I understand why this message is happening. When googling for a solution, the results that appear recommend throwing each element in a row in turn. However, this becomes impractical if you need to combine a dozen elements:
std::string("x:") + x + std::string(", y:") + y + std::string(", width:") + width + std::string(", height:") + height + ...;
What is the “correct way” in C ++ to concatenate strings, characters, char arrays, and any other data that can be converted into one string? Is there something like Python that beautiful string formatting functions in C ++?
source
share