0

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;

}

Jendas
  • 3,359
  • 3
  • 27
  • 55
TheAsker
  • 11
  • 1
  • 2
    Take a look at the ASCII-table. `'0'` is not `0`. – Deduplicator Oct 03 '14 at 20:15
  • It looks like you are forcing the INT to a string. Which you probably should convert, but C is doing it for you. If you don't convert it, it stays an INT. – durbnpoisn Oct 03 '14 at 20:15
  • 2
    Your current code will work (only) with values of `i` from 0 through 9. For any value >= 10, it'll run into problems again. `std::to_string` *should* work correctly though (if you have problems with it, post the code where you tried to use it, and had problems). – Jerry Coffin Oct 03 '14 at 20:17

2 Answers2

1

i+'0' here is adding the ASCII value of the character '0' to the value of i
i+'0' is equal to the ASCII code of the character '2'
so you are concatenating str to a number (at my computer it is 50) (which is different than 2 but this is the ASCII code for 2)
(note that 50 is based on my computer's ASCII table and might not be the same for you - I am not sure if it is unique for all)
and the character whose ASCII code is 2 is not '2', it is that odd character you got

PhpLou
  • 430
  • 3
  • 16
  • 1
    "(note that 50 is based on my computer's ASCII table and might not be the same for you - I am not sure if it is unique for all)" WTF! how many ASCII table are you heard of? – Emilio Garavaglia Oct 03 '14 at 21:04
  • @EmilioGaravaglia I said I am not sure so I don't give possibly wrong information and anyway I don't encourage relying on these values but use `+'0'` or `+'A'-1` or `+'a'-1` and the fact that I didn't heard of another one doesn't mean it doesn't exists (theoretically) – PhpLou Oct 03 '14 at 23:02
  • @PhpLuo: Since you are not sure you GAVE wrong information, since you make the OP to believe there are many ASCII around. That THEORETICALLY cannot be by definition. About "encouraging" or not a bad practice" or not, it mostly depend how your code is subject to localization or is itself A localization. Just look at the library strtoi function source code: what you are discouraging in exactly in the very core of the standard library. – Emilio Garavaglia Oct 04 '14 at 08:14
0

This is pure luck ;-)

Your code add i to the ascii value of '0' (=> 48).

In your sample 2+48 = 50 . And luckilly 50 is the ascii value for '2' so it works !!

If you try with 'a' instead of '0' (str += i+'a';) you will obtain a 'c' at the end of your string.

So, do not do that ;-)

The correct way to add an integer is discussed here :

How to concatenate a std::string and an int?

Good Luck !

Community
  • 1
  • 1
DomDom
  • 76
  • 5
  • 1
    well... luck ... Adding '0' to an int is part of the int to text conversion routine of the classic locale. Not a good thing at high level, but low level conversions work that way. – Emilio Garavaglia Oct 03 '14 at 21:03
  • 1
    sure, you're right but in this case if he tries with int i=20; this is not gonna work that's why he's "lucky" with its sample – DomDom Oct 03 '14 at 21:09
  • ??? I see i+'0' after i=2. and 2+'0' == '2' is true by definition, being numerical digits consecutive codepoints. Where did you read 20 ?? – Emilio Garavaglia Oct 03 '14 at 21:19
  • I did not read 20 but his question was : " I was trying to add an int to the end of a string." . 20 is an example of int that will not work with his sample. But i don't wanna argue, you're experienced and smart. I'm sure you get my point. – DomDom Oct 03 '14 at 21:24
  • 1
    yes: it all boils down to the meaning of "adding an int" (in OP intentions), and how conscious the OP is about the meaning of '0' in an arithmetic expression. We are just assuming different starting conditions. – Emilio Garavaglia Oct 04 '14 at 08:21