在此问题中,输入是一个长度为10 ^ 6的字符串,它必须将数字加起来。我的问题:在每个循环中都添加了数字48,但我不知道原因。如果在第十行使用“ soma = soma + nome [x]-48”,则可以解决该问题。但是我想知道为什么会这样。谢谢
#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];
//cout << "The current value of soma is " << soma << endl;
}
cout << soma;
return 0;
}