
please wait
total = number_string[0] + number_string[1] + number_string[2];
. number_string[0]
is a character. it have a ascii value for its corresponding character. if you want to let it become an actual number for the character just minus '0' to number_string[0]
. since you have three number_string
so you have to subtract 3 * '0'. replace your original statement to this. total = number_string[0] + number_string[1] + number_string[2] - 3 * '0';
'1' - '0'
can actually get 1. since the ascii is placed in order that smaller number in former. larger number in later. for example '0' is 48(in decimal). '1' is 49(in decimal). '2' is 50(in decimal) and so on to '9' is 57(in decimal). since the order of the character. you can apply this to other character. '2' - '0' is 50 - 48. it is equal 2 and so on. c++ do the implicit conversion for you.