concat two chars into a string?
How to concat two chars into a string?
1 2 3 4 5 6 7 8 9 10 11 12
|
#include <iostream>
int main()
{
std::string myString;
//output
myString = '2';
std::cout << "myString: " << myString << std::endl; //2
myString = '2' + ' ';
std::cout << "myString: " << myString << std::endl; //R, why??
}
|
output:
Why is "R" printed?
Thank you.
Because a char is an ASCII code:
'2' = 50
' ' = 32
R = 50 + 32
Thanks megatron. That makes sense.
This works as expected:
1 2 3 4 5 6 7 8 9 10 11
|
#include <iostream>
int main()
{
std::string myString;
//output
myString = '2';
myString += ' ';
myString += '3';
std::cout << "myString: " << myString << std::endl; //2 3
}
|
output:
Excellent work mate, glad you could figure it out. Just remember that variables are variables, and if they can be added, they will.
Topic archived. No new replies allowed.