I'm trying to figure out a code that a friend sent me and I couldn't figure out why this certain code exists.
The goal is that when you type a 3 digit number sequence in string, it converts it to int and prints the sequence in reverse.
string s; cout <<"enter integer sequence"; cin >> s; int firstdig, seconddig, thirddig; firstdig = s[0]; seconddig = s[1]; thirddig = s[2]; cout << thirddig << seconddig << firstdig; Here is the problem with this code
When I input "123", the output becomes "515049" instead of "321"
However
This is the code that apparently fixes the problem
string s; cout <<"enter integer sequence"; cin >> s; int firstdig, seconddig, thirddig; firstdig = s[0] - '0'; seconddig = s[1] - '0'; thirddig = s[2] - '0'; cout << thirddig << seconddig << firstdig; This time, I input "123", the output becomes "321"
My main question is, where did "515049" come from, and what does the code "- '0'" do? I don't know what that code does. C++ C++ C++
char, notint.