Sum of digits of a string only works when I do a subtraction I don’t understand

Asked

Viewed 332 times

1

The program receives a string size up 10^6 and add the digits. I made the code, but to each loop 48 is added to the sum variable. If I modify the tenth line as soma = soma + nome[x] - 48 the problem is solved. However, I would like to understand why this occurs.

#include <iostream>
#include <cstring>
using namespace std;

int main(){
   char nome[1123456];
   long long soma = 0;
   cin >> nome;
   for(int x = 0; x < strlen(nome); x++){
       soma = soma + nome[x] - 48;
        //cout << "O valor atual da soma eh << soma << endl;
    }
    cout << soma;
    return 0;
}

1 answer

1


You’re getting a guy char which for all intents and purposes is an integer, but it is not a digit. It can be used to print any text, but it is a number. It just has to special that a text of a character is associated with each number and this can be printed to show the user so.

A very common confusion that most people make (I know a lot of experienced programmers who don’t understand this) is that a number is different from its textual representation. Everything you see on the computer that looks like a number that you know is not a number, it’s just a text that represents that number in a way that a human understands. The number exists by itself. The same goes for input data, you enter characters that are numeric digits, but you do not enter the number. At most it has functions that makes this conversion for you.

When you take a character of the typed text is taking a text that is not a number, after all everything in the computer is number. The text is obtained through a thing called ascii table. It establishes the relationship between the numbers that are stored and the characters that they represent verbatim. So just like 65 is the letter A, 48 is the character 0 (you can consult the table on link above). Note that the character 0 does not equal the number 0 when you want the given character number. That number is 48. When you pick the text is not picking up the number, you are picking up the character, to convert to the number you have to subtract 48, so the character 0 turns to number 0, since 48 - 48 is 0. E 1 which is worth 49 gives 1, since 49 - 48 gives 1, and so goes up to 9.

When you subtracted, you solved this problem and made this algorithm correct.

It does not mean that you have arranged all the problems, for example there is no guarantee that the person has not typed a character that is not a numeric digit. I also think I should have used one string of C++ and not of C, even more so using strlen() that will give a very bad performance in this code.

  • One more thing: so how would I ever convert these representations of numbers by numbers really? So I don’t have to subtract 48. And thank you very much for your reply :)

  • Nothing, this is how it is done, this is the conversion, in a simple and efficient way. The most correct would be to use an iterator in string, you don’t need to use that at(). This is better: https://ideone.com/RLay8P

Browser other questions tagged

You are not signed in. Login or sign up in order to post.