I do not know where is the condition
One real number is entered which does not exceed 105105
that is, the limitation on the absolute value, since it does not play any role.
Without checking this condition (which you yourself can insert into the program), the program may, for example, look like this
#include <iostream> #include <cmath> int main() { while (true) { const int Base = 10; const int N = 3; std::cout << "Enter a float number with " "a fraction of at least " << N + 1 << " digits (0 - exit ): "; double d; if (!(std::cin >> d) || d == 0) break; double int_part; double fraction; fraction = std::modf(std::fabs( d ), &int_part); int sum = int(fraction * Base) % Base + int(fraction * Base * Base) % Base + int(fraction * Base * Base * Base) % Base; std::cout << "The sum of " << N << " digits of the fraction is " << sum << std::endl; } }
Indeed, if you consistently enter the values 123.4567 and 42.4242, the output to the console will look as intended in your task
Enter a float number with a fraction of at least 4 digits (0 - exit ): 123.4567 The sum of 3 digits of the fraction is 15 Enter a float number with a fraction of at least 4 digits (0 - exit ): 42.4242 The sum of 3 digits of the fraction is 10 Enter a float number with a fraction of at least 4 digits (0 - exit ): 0
Instead of the modf function from <cmath> you could just write
double fraction = std::fabs( d ) - int( std::fabs( d ) );
to remove the integer part of the number.