digits are really problematic

How do you let the program know how many digits a given integer has?
¿how do you do it by hand?
(int)(log10(value)) + 1?

I wouldn't do THAT by hand, though...
Last edited on
¿why not? int_log10() is quite easy
Unless value is 0.
You can turn it into a string and get the size of the string.
You can divide it by ten until it's zero, counting the number of divisions it took.
You can compare it with other numbers (if it's bigger than 9 and less than 100, for example, it has two digits).

As ne555 said, how would you do it on paper? Do that. If you would write the number on the paper (so it's a string of characters, yes?) count the number of digits, then the first option I gave is what you're doing.
I would just ask someone else.
1
2
3
4
5
6
int digits(int n){
    std::cout << "How many digits does this number have? " << n << std::endl;
    int ret;
    std::cin >> ret;
    return ret;
}
I could render the number on screen in a fixed-width font, measure its width, and use that and the width of a a single character to deduce how many character were in it.
Topic archived. No new replies allowed.