I've posted the relevant parts of the code I was working on below. At first, I was trying to add an integer to the end of a string.
However, none of the methods I found were working quite right (to_string, itoa, casting). Whenever I simply added the integer to the string, I would get the string plus an odd little symbol at the end, such as smiley face or spade. However, when I add a '0'
to the line str += i
, it works!
The problem is, I have no idea why. I was hoping someone would be willing to explain to me what exactly is going on here and why it works? I just don't understand how I can add integer to the string without a cast, and why adding a char (I think?) makes it work.
Thanks to everyone who takes the time to read this.
int main()
{
string str = "Filler";
int i = 2;
str += i+'0'; //if I remove the +'0' it no longer works as intended.
cout << str << endl;
return 0;
}