How many digits?

This is a discussion on How many digits? within the C++ Programming forums, part of the General Programming Boards category; Hi all! I need to find a way of calculating the number of digits in an integer, here's a glimpse ...

  1. #1
    Registered User JM1082's Avatar
    Join Date
    Mar 2011
    Posts
    51

    Cool How many digits?

    Hi all!

    I need to find a way of calculating the number of digits in an integer, here's a glimpse of what I've got so far:
    Code:
        if ( ( userdigit / 1 ) < 1 )
        {
            cout << "1 digit integer!";
        }
        else if ( ( userdigit / 10 ) < 1 )
        {
            cout << "2 digit integer!";
        }
        else if ( ( userdigit / 100 ) < 1 )
        {
            cout << "3 digit integer!";
        }
        else if ( ( userdigit / 1000 ) < 1 )
        {
            cout << "4 digit integer!";
        }
        else if ( ( userdigit / 10000 ) < 1 )
        {
            cout << "5 digit integer!";
        }
    ... And variants of, none of which have been any success!

    Does anybody have a good way of doing this?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Careful use of log10 from <cmath> could yield what you desire. Alternatively, you could loop with a similiar idea as to what you showed in your example code.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,307
    I urge you to take a good hard look at that first line. Really think about it for a bit. What is the result of dividing by one going to be?

    You certainly can do it somewhat along the lines of what you have, but you definitely haven't quite got it right.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User JM1082's Avatar
    Join Date
    Mar 2011
    Posts
    51
    OMG!!! I'm such a thicko!!!
    :-)

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,307
    It's merely a case of having an idea, and then coding it up simply by following a pattern, and not stopping to realise that it's a little off.

    The first one is in fact easy, you know exactly what to write, it's when the value is less than ten. From there you can actually easily derive a recursive solution if you wanted to, since if it was not less than 10 then you can divide it by ten and test again, knowing that if it's less than ten this time then there were two digits, and so on.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to add 2 digits?
    By nefsan in forum C Programming
    Replies: 16
    Last Post: 03-28-2008, 02:41 PM
  2. Digits...
    By Murk in forum C Programming
    Replies: 8
    Last Post: 02-05-2008, 01:16 PM
  3. get all the right digits
    By mag_chan in forum C Programming
    Replies: 6
    Last Post: 11-27-2005, 05:16 AM
  4. Two digits
    By cyberCLoWn in forum C++ Programming
    Replies: 4
    Last Post: 02-23-2004, 12:53 PM
  5. Getting digits from int.
    By aker_y3k in forum C++ Programming
    Replies: 8
    Last Post: 02-21-2003, 11:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21