So I have an assignment of adding up all of the digits of a number for example of 3256 would be 3+2+5+6 And so what I did was change the number into a string first and add all of the numbers of the string. However, the problem is that the string only takes them as chars and prints everything for example my number is 2^352 = 9.1779 e+105
So my array[1] = .
THis is what i have so far:
int calcular2()
{
double r;
r = pow(2, 352);
n = 0;
printf("2^352 es: %0.0f\n", r);
std::ostringstream ostr;
ostr << r;
std::string resultado = ostr.str();
for (int j = 0; j < 127; j++)
{
if (resultado[1] == '.')
continue;
n = resultado[j] + n;
}
printf("La suma de todos los digitos de 2^352 es:");
printf("%d\n", n);
return 0;
}
So what I dont understand is how to add up all the numbers in resultado[j] and so that it doesnt consider r as 9.17799e+105 since resultado[8] prints as e and so far.