Thread: Reading number of digits

  1. #1
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174

    Reading number of digits

    I need my program to be able to read how many digits are in a number that is input by the user.

    For example, I'm looking for something like:
    Code:
    if (digits = 10) {
    .... do something....
    } else {
    ... do something else....
    }
    Of course, the parts in brackets are not correct code for c. I just don't know how to interpret this from English to C.

    Thanks for any help!

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Use the following algorithm.

    1) Obtain the last digit of the number by calculating nr % 10.
    2) Divide the number by 10.
    3) Repeat until the result of the division to 10 equals 0. (no more digits)

    Store each digit in an array or something. At the end of this algorithm the array will contain the digits in reverse order. So if you need them in the right order make sure to print the array in reverse.

    If you don't need to store/use the digits than you can just count how many times you can divide the number by 10 until it equals 0.
    Last edited by claudiu; 03-25-2010 at 08:08 PM.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    Sorry that's not quite what I'm looking for. I should have been more clear.

    I have a large number (a barcode) that I have used getchar to find each digit. However, sometimes the user might not include the 13th digit in a barcode, because that is a checksum digit that I will need to calculate.

    The problem is, I can't do the division by 10 and use a counter because I have declared each digit rather than the entire barcode number as a whole.

    So how can I translate:

    Code:
    if (thirteenth digit exists)
    .....
    else
    .....

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Ok, let me just backtrack a bit. Are you saying that the user inputs 12 characters which are digits and may input a 13th digit or another character? If so check if that character is a digit by attempting to to convert it into an integer with atoi(). If atoi fails it will return 0 and then you will know it's not an integer.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Quote Originally Posted by Mentallic View Post
    Sorry that's not quite what I'm looking for. I should have been more clear.

    I have a large number (a barcode) that I have used getchar to find each digit. However, sometimes the user might not include the 13th digit in a barcode, because that is a checksum digit that I will need to calculate.

    The problem is, I can't do the division by 10 and use a counter because I have declared each digit rather than the entire barcode number as a whole.

    So how can I translate:

    Code:
    if (thirteenth digit exists)
    .....
    else
    .....
    So how are you reading in the data? If you're reading in character-by-character, well, then you know how many characters you read and we're done.

  6. #6
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    No, the user may input a 12 or 13 digit number. I do have the issue with when they're character rather than digits, but I've asked for help with that problem in another thread.

    I am finding the digits separately in that large 12/13 digit number with getchar(). I just don't know how to test whether there are 13 digits or not.

    Maybe something like:

    Code:
    if (thirteenth_Digit >= '0' && thirteenth_Digit <= '9') {
        thirteenth_Digit = getchar ();
    } else {
        ......
    }

  7. #7
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    Quote Originally Posted by tabstop View Post
    So how are you reading in the data? If you're reading in character-by-character, well, then you know how many characters you read and we're done.
    How do I know how many characters I've read?

  8. #8
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    If you are reading characters one by one just a use a counter. I really don't see what your actual problem is. Maybe you should post the code you have so far and leave commented the part you can't work out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. decimal to number if digits in different bases
    By jorgejags in forum C Programming
    Replies: 21
    Last Post: 09-24-2008, 12:55 PM
  4. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 08:43 PM
  5. Finding digits in a number
    By MadStrum! in forum C Programming
    Replies: 5
    Last Post: 09-05-2002, 03:05 AM