C++ Help

May 22, 2019 at 10:23pm
Line 19: number_string[0-2] are chars, not integers. You're adding the ASCII values of the chars together.
May 22, 2019 at 10:27pm
the problem occur at 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';
Last edited on May 22, 2019 at 10:36pm
May 22, 2019 at 10:51pm
consider you number_string[0] is '1' after input. '1' has ascii value 49(in decimal) and '0' has ascii value 48(in decimal). you use '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.
Last edited on May 22, 2019 at 11:00pm
Topic archived. No new replies allowed.