Thread: How many digits?

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,318
    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,318
    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, 02:16 PM
  3. get all the right digits
    By mag_chan in forum C Programming
    Replies: 6
    Last Post: 11-27-2005, 06:16 AM
  4. Two digits
    By cyberCLoWn in forum C++ Programming
    Replies: 4
    Last Post: 02-23-2004, 01:53 PM
  5. Getting digits from int.
    By aker_y3k in forum C++ Programming
    Replies: 8
    Last Post: 02-21-2003, 12:45 PM