count numbers input

This is a discussion on count numbers input within the C Programming forums, part of the General Programming Boards category; Can anyone tell me about how can i check the number of digits which a user has just put in ...

  1. #1
    Unregistered
    Guest

    count numbers input

    Can anyone tell me about how can i check the number of digits which a user has just put in
    for example
    input: 21212
    number of digits = 5

    thanks

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Depends how there stored.

    Assuming they're in a string, you can use strlen(), or if you want to validate that they are actually 5 numbers (as opposed to 4+ 1 letter), you'll have to try something like

    - looping through the array, checking each one with isdigit()
    - convert to a numeric variable via atoi() or atol() and check the return code to make sure it worked.

    I'm sure there's a few other ways too.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    you could also use -->

    Code:
    if(num>=0 && num<=9)
     digits=1;
    if(num>=10 && num<=99)
     digits=2;
    if(num>=100 && num<=999)
     digits=3;
    //so on..

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Usually it's easiest to place the number in an array and count the valid elements, but if you want to count the digits of a number without converting it to an array you can make a copy and break it down, counting each digit:
    Code:
    /* get input and make a copy */
    for ( x = 0; copy != 0; x++ )
      copy /= 10;
    printf ( "%d contains %d digits\n", input, x );
    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    If you want to get complicated/fancy and you are only talking about integer input (no floating point stuff), you can use logarithms to do this:
    Code:
    #include <cmath>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int iValue = 99999;
        cout << "Number of digits for " << iValue << " is "
             << floor(log10( iValue )+1.0) << endl;
    }
    Outputs 5, change iValue to 100000 for example and it outputs 6. Change it to whatever you want and see how it behaves. Like I said, the caveat is that this only works for integers.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Crap... sorry about that, I thought I was on the C++ board. Anyway, my response still applies:
    Code:
    #include <math.h>
    #include <stdio.h>
    
    int main()
    {
        int iValue = 99999;
        printf( "Number of digits for %d is %d\n", iValue,
                 (int) floor(log10( iValue )+1.0) );
        return 0;
    }
    Last edited by hk_mp5kpdw; 05-10-2002 at 10:51 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 04:25 PM
  2. EOF messing up my input stream?
    By Decrypt in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2005, 04:00 PM
  3. Reading / input column of numbers into an array
    By boyfarrell in forum C Programming
    Replies: 6
    Last Post: 08-28-2005, 11:24 AM
  4. Replies: 3
    Last Post: 11-17-2002, 04:51 PM

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