Working with a number

This is a discussion on Working with a number within the C Programming forums, part of the General Programming Boards category; I need to read in a 13 digit number which is barcode, and then I need to spit it out ...

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

    Working with a number

    I need to read in a 13 digit number which is barcode, and then I need to spit it out with hyphens added after the 3rd, 9th and 12th digit.

    So if the user inputs barcode 1234567890123 then my program should output 123-456789-012-3

    I'm learning how to code in a class and so we are restricted with what functions and whatnot we can use, so please bare with me.

    What I have so far:

    Code:
    #include <stdio.h>
    #include <assert.h>
    
    #define UPPER_LIMIT 10000000000000LL
    #define LOWER_LIMIT 1000000000000LL
    #define FIRST_HYPHEN 10000000000LL
    #define SECOND_HYPHEN 10000
    #define THIRD_HYPHEN 10
    
    int main(int argc, char *argv[]) {
    
        long long barcode = 0;
        int numScanned = -1;
        long long remainder = 0;
    
        printf("Enter your barcode: ");
        numScanned = scanf("%lld", &barcode);
        
        //Check that assumptions have been followed
        assert( (numScanned == 1) && ((barcode / LOWER_LIMIT) != 0) );
        
        remainder = barcode;
        
        long long i = LOWER_LIMIT;
        
        //Loop to get digits of barcode
        for (i = LOWER_LIMIT; i > 0; i = i / 10) {
            if ( (remainder / FIRST_HYPHEN == 0)
                || (remainder / SECOND_HYPHEN == 0)
                || (remainder / THIRD_HYPHEN == 0) ) {
                printf("-");
            } else {
                printf("%lld", remainder / i );
            }
            remainder = remainder % i;
        }
        
        printf("\n");
        
        return 0;
    }
    And it took me a while to figure out why with input 1234567890123 I'm receiving output 123-----------, but I realized it's because in my loop near the end,

    Code:
     if ( (remainder / FIRST_HYPHEN == 0)
                || (remainder / SECOND_HYPHEN == 0)
                || (remainder / THIRD_HYPHEN == 0) ) {
                printf("-");
            } else {
                printf("%lld", remainder / i );
            }
    This if statement means that it prints a hyphen after the 3rd digit as expected, but after that, the remainder is 1 digit less, but remainder / FIRST_HYPHEN == 0 is still true. What I intended for that if loop to do was to only be true if the remainder was 10 digits, 4 digits and 1 digit long. Any suggestions as to how I can work around this?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why don't you just read it as a string? Or, sprintf it so that it ends up a string, then sprintf again to get it the way you want.

    edit - remainder is done by the % operator.
    Code:
    finaldigit = bignum % 10;
    bignum /= 10;
    threemore = bignum % 1000;
    bignum /= 1000;
    ...
    Just check once to make sure it's 13 digits, and do all the math, making bignum smaller and smaller until it's down to your 123.

    Quzah.
    Last edited by quzah; 08-10-2011 at 04:46 AM.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    We haven't learnt how to use strings and whatnot in class yet, so we aren't allowed to use them. Also, another restriction added to us is that we have to read in the barcode as a single number of type long long, and not read each digit in.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    What is the operator bignum /= 10?

    And yes I realize the mod function will give me the last digits, but it's the sections of the barcode in between the hyphens which I'm having trouble with.

    edit: Ahh ok I'll try work with the mod function and work backwards.
    Last edited by Mentallic; 08-10-2011 at 04:57 AM.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Is there a reason you are using a loop?
    Code:
    bignum = you reading a bignum and making sure it's the right length
    firstthree = divide big number by 10000000000
    nextsix = divide big number by 10000, then mod by 1000000
    nexthree = divide big number by 10, then mod by 1000
    lastone = mod big number by 10
    printf( "%3d-%6d-%3d-%1d", firstthree, nextsix, nextthree, lastone );

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    I've tried that at first but a big problem I encountered was when 0's were present in the barcode at the start of nextsix or nextthree then it ignores them, so rather than an output of 123-456789-012-3 it gives 123-456789-12-3.

    This is the main reason why I started using a loop.

  7. #7
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    Ok I answered the problem without looping, but I know this method will break down and become useless for the later stages of the assignment. Anyway, I'll cross that bridge when I get to it. Thanks quzah!

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Mentallic View Post
    I've tried that at first but a big problem I encountered was when 0's were present in the barcode at the start of nextsix or nextthree then it ignores them, so rather than an output of 123-456789-012-3 it gives 123-456789-12-3.

    This is the main reason why I started using a loop.
    Change the %3d to %03d. (Read up on how to format your test with prinf and it will cover it.)


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    Oh I totally forgot about the extra 0's added by %03d, that's a great idea!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 03-30-2009, 05:09 AM
  2. loop to get greatest and least number, its not working please help
    By stressedstudent in forum C++ Programming
    Replies: 27
    Last Post: 09-27-2007, 04:45 AM
  3. MergeSort function not working with randomn number
    By Madshan in forum C++ Programming
    Replies: 5
    Last Post: 02-12-2005, 01:15 AM
  4. Number of words multiplied by the number of lines in Linux
    By sjalesho in forum Linux Programming
    Replies: 2
    Last Post: 11-20-2003, 03:25 PM
  5. working out the multiple of a number
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-22-2001, 12:23 PM

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