digits are really problematic

Feb 9, 2019 at 2:11am
How do you let the program know how many digits a given integer has?
Feb 9, 2019 at 2:15am
¿how do you do it by hand?
Feb 9, 2019 at 3:23am
(int)(log10(value)) + 1?

I wouldn't do THAT by hand, though...
Last edited on Feb 9, 2019 at 3:24am
Feb 9, 2019 at 7:49am
¿why not? int_log10() is quite easy
Feb 9, 2019 at 8:39am
Unless value is 0.
Feb 9, 2019 at 10:33am
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.
Feb 9, 2019 at 10:43am
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;
}
Feb 9, 2019 at 11:42am
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.