tags:

views:

123

answers:

4

Hi,

The assignment at my first year uni computing course says my program should read a barcode number, and then display the same 13 digit barcode number separated by hyphens. For example, 9300675016902 should look like 930-067501-690-1.

The restrictions say I can't use the following:

No arrays No strings No functions.

Any directions on this?

So far I have:

part1 = barcode/10000000000;

which gives me the first three digits, and this:

part4 = barcode%10;

which gives me the last digit.

Thanks in advance!

A: 

I'll give you a clue, without answering your homework directly.

Bit masks.

ChrisBD
How do you suggest to extract DECIMAL digits using bit masks?
qrdl
Barcodes are read as ASCII text, therefore available in a string. One byte per character.If the barcode has already been converted into a number then that is a different matter.
ChrisBD
+1  A: 

Try:

    long p1 = n/10000000000;
    long p2 = n%100000000000/10000;
    long p3 = n%10000/10;
    long p4 = n%10;
    printf("%03ld-%06ld-%03ld-%01ld\n",p1,p2,p3,p4);
codaddict
Hmm, that uses strings though. Taking his assignment literally, he should probably convert to ASCII by adding 48 to each individual digit.
Michiel Buddingh'
Better yet, he should convert to ASCII by using '0' instead of 48. For some of us, the ASCII table is easier remembered in hex rather than decimal (0x30 == '0' == 48).
Thomas Matthews
A: 

If you actually can't use strings, you're going to need to do is to go through each digit in turn and use putchar('0' + x); where x is the current digit..

Turtle
+1 for using '0' rather than its numerical equivalent.
Thomas Matthews
A: 

codaddict, your code works but I get a warning: integer constant is too large for 'long' type

any ideas?

dydx