Thread: figuring out size of numbers

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    153

    figuring out size of numbers

    Hey everyone,
    I was curous, without using library functions, how I could go about figuring out how many digits are in a number. I'm doing this to test some user input and print in error message if they enter any number that doesn't have 4 digits. Any suggestions? Thanks a lot!

  2. #2
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    There are several options; I'll tell you about three (assuming integer and not floating point numbers). One is slightly abstract, one requires conversion, and one requires math.

    Using math: divide by ten until the value is zero; the number of times divided by ten is the number of digits in your value. E.g., 123 / 10 = 12 (count = 1), 12 / 10 = 1 (count = 2), 1 / 10 = 0 (count = 3), 3 digits total.

    Using conversion: convert the value to a string and determine the length of the string. E.g., 123 =(convert)=> "123" =(strlen)=> 3.

    The abstract (and most interesting): take the logarithm of the number, cast to int, and add 1. E.g., log(123) = 2.0899051114393979318044397532233 + 1 = 3

    Btw, why can't you use library functions? Instructors instructions?

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Another approach is to analyze the binary of the integer if you are referring to the data representation via bitwise.

    Kuphryn

  4. #4
    Banned
    Join Date
    Jun 2005
    Posts
    594
    or why not


    if you just need to know if its 4 digits,
    it prolyl be faster, just to do

    Code:
    if(number <= 9999 && number >= 1000)

    as long as number is an int, it should work just fine.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    the problem with that approach vectors is if the user enters 0003, which is a legal 4 digit number but fails the test your describe...just a random observation. Thanks for the reply everyone, I feel pretty good about Lucky's answer. The only thing I wonder though is say the user enters 0003, would the divide by 10 method work?

    EDIT:
    And the answer is no...it will not...so my new question how could I correct this bug, once again without library functions? Oh and by the way Lucky, this is just a program I was typing up for the sake of it, I want to do this without library functions. Thanks again!
    Last edited by Chaplin27; 08-04-2005 at 06:13 PM.

  6. #6
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    I think in the case of numbers with leading zeros.. the only sure-fire method described so far would be the conversion to string.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    I see brain...it makes the most sense...I was just trying to solve it mathematically only, without any containers...just the numbers and myself, man vs. machine!!! Whoa...got a little carried away...ahem *clears throat*...yeah I guess a string is the best option...math has failed me again....

  8. #8
    Banned
    Join Date
    Jun 2005
    Posts
    594
    ahh i didnt even think of it like that.

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    String is really the only option. You will have to read your number in as a string (otherwise, the fact that it had leading zeros is lost), but you'll still have to ensure that it is a valid number (easy using library functions -- but not that hard without), and then simply count the number of characters in the string.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  10. #10
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Code:
    if(sizeof(MyStringNumber) == (sizeof(char) * 4))
    {}
    Out of sheer curiosity (cause I'm not too good when it comes to these kinds of things), would this work?

    EDIT:
    Added second sizeof()
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  11. #11
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    No... this will not work.

    If the char array was statically declared within the current scope, then your sizeof will return the size of the array (times sizeof(char)), but this is not necessarily (and probably not) the size of the string. If the array was not declared in the current scope, or if it was dynamically allocated, then all you get is the size of a pointer-to-char.

    Best just to look for the '\0' character.

    (Of course, assuming you used the library I/O, and didn't write your own in assembly or something of that nature ).

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  12. #12
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    That makes perfect sense, and here I thought I was clever Ah well, another day.

    Thanks for clearing it up.
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  13. #13
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    I was reading this thread, and just for my info,how can I convert an int to a string.
    If I have an variable int x=275, I want to turn it into CString x = "275". Or a char x[] = "275".

    Thanks.
    Everything is relative...

  14. #14
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Lightbulb #include<cstdlib>

    int-to-ascii conversion:

    Code:
    int x1 = 275;
    
    char x2[20];
    
    itoa(x1, x2, 10); 
    
    cout << "There are " << strlen(x2) << " digits in the number " << x1;
    Last edited by The Brain; 08-05-2005 at 10:36 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Why is 0003 a four digit number? It looks like a 1 digit number to me, but if your requirements are to find out how many digits typed by the user then the string version will work.

    You would have to read it in as a string in the first place because if you read it in as a number and then convert it to a string you will lose the leading zeroes.

    Also, if you do want to convert a number to a string, stringstreams are a more portable and safer way to do so than itoa, which is not standard.

    earth_angel, if you are using a CString (which is an MFC string and so not standard anyway), use the CString Format function (e.g. x.Format("%d", 245);)
    Last edited by Daved; 08-05-2005 at 10:19 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 01:30 PM
  2. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  3. char problem
    By eXistenZ in forum Windows Programming
    Replies: 36
    Last Post: 02-21-2005, 06:32 AM
  4. Replies: 11
    Last Post: 03-25-2003, 05:13 PM
  5. programming with random numbers
    By xstudent in forum C Programming
    Replies: 13
    Last Post: 05-21-2002, 01:36 AM