Can you explain why 49 is added at the end? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can you explain why 49 is added at the end?

I've been trying to convert a string to an int. but when I use [cout<<(int)msg.front();] it adds 49 at the end of the now converted int. But when I remove the [cout<<] to make it just [(int)msg.front();]. why does it add numbers that I have not entered before? https://code.sololearn.com/cTPbLWiIVGMf/?ref=app

3rd Jun 2018, 7:02 AM
Mugisha Benjamin Safari
Mugisha Benjamin Safari - avatar
3 Answers
+ 2
because you are interpreting the number which represents the first char as int, not converting the char to int, you need to subtract 48, the type of cast you are using just tells the compiler to treat whatever is in them memory there as an int, not to do a string to int conversion. you might want to look into atoi() and related methods. if you input more then one digit this wont work anyways since front gives you a reference to the first char, which contains the ascii code that represents the first digit
3rd Jun 2018, 10:02 AM
Max
Max - avatar
0
How do I arrive at your error? Instructions unclear, not sure what to enter.
3rd Jun 2018, 7:18 AM
Andre Daniel
Andre Daniel - avatar
0
Find the comment where it says >convert string input to int< and just add cout<<. Run it and enter 20 digits to see what happens. PS: the added numbers aren't always 49. they may be different depending on what numbers you input. But so far they seem to be only two digi numbers like 46, 53 or 60( they are not the actual numbers I've found).
3rd Jun 2018, 7:25 AM
Mugisha Benjamin Safari
Mugisha Benjamin Safari - avatar