# Thread: Working with a number

1. ## 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;

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. 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.

3. 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. 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.

5. 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.

6. 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. 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. Originally Posted by Mentallic
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.

9. Oh I totally forgot about the extra 0's added by %03d, that's a great idea!