0
# include<stdio.h>
# include<string.h>
# include<stdlib.h>


int

main(int argc, char **argv){

// Declare Variables
char      barcode[50];	
int       count;
long long intBarcode;
long      countryCode;
int       dig0, dig1, dig2, dig3, dig4, dig5;
int       dig6, dig7, dig8, dig9, dig10, dig11;
long long num;
long      rem, sum;
int       checksum;
long long manufac, product, language, publish, title;
char      country[50];
int       k, j, i = 0;

    
    // Get input from user as type string
    printf("Enter Barcode: \n");
    
    count = scanf( "%s", barcode);
    
    // Check validity of input
    if (count != 1){
        printf("not a valid input");
        return 0;
    }
    // Remove Hyphens
    for (i = 0; i < sizeof(barcode); i++){
        if (barcode[i] == '-' || barcode[i] == ' '){
            k = j = i;
            for (j < sizeof(barcode); j++;){
                barcode[k] = barcode[k + 1];
                k++;
    }}}
    
    // Convert barcode to long long integer
    intBarcode = atoll(barcode);
    
    
    // Determine Country Code
    if (intBarcode > 999999999999LL){
        countryCode = intBarcode % 10000000000LL;
        
        num = intBarcode/10;

    } else {
        countryCode = intBarcode % 1000000000LL;
        
        num = intBarcode;
    }   
    	    
   

	// Calculate checksum				              
    dig11 = num%10;
    num = num/10;
    dig10 = num%10;
    num = num/10;
    dig9 = num%10;
    num = num/10;
    dig8 = num%10;
    num = num/10;
    dig7 = num%10;
    num = num/10;
    dig6 = num%10;
    num = num/10;
    dig5 = num%10;
    num = num/10;
    dig4 = num%10;
    num = num/10;
    dig3 = num%10;
    num = num/10;
    dig2 = num%10;
    num = num/10;
    dig1 = num%10;
    num = num/10;
    dig0 = num%10;

    
    sum = ((dig0 + dig2 + dig4 + dig6 + dig8 + dig10) + (3*(dig1 + dig3 + dig5 + dig7 + dig9 + dig11)));
    rem = sum%10;
    checksum = 10 - rem;
	
            
    if ((intBarcode > 999999999999LL) && (checksum != (intBarcode % 10))){
        printf("Not a valid EAN barcode: Checksum incorrect");
//        return 0;
    }
		 
		
		
		    
    // Determine other parts of the barcode
    // EAN
    if (countryCode > 849 && countryCode < 977){                    
                                                                
    manufac = (100000*dig3 + 10000*dig4 + 1000*dig5 + 100*dig6 + 10*dig7 + dig8);
    product = (100*dig9 + 10*dig10 + dig11);

    // ISBN
    } else if (countryCode > 976 && countryCode < 980){               

    language = (10*dig3 + dig4);
    publish = (1000*dig5 + 100*dig6 + 10*dig7 + dig8);
    title = (100*dig9 + 10*dig10 + dig11);
    }
    
    // Convert back to type string
    sprintf(barcode, "%lld", intBarcode);
    
    
    // Hyphenate
    // EAN
    if (countryCode > 849 && countryCode < 977){

		memmove(barcode + 3, barcode + 2, 20);		               
		memset(barcode + 3, '-', 1);
		memmove(barcode + 10, barcode + 9, 20);
		memset(barcode + 10, '-', 1);
		memmove(barcode + 13, barcode +12, 20);
		memset(barcode + 14, '-', 1);

    // ISBN
    } else if (countryCode > 976 && countryCode < 980){

		memmove(barcode + 3, barcode + 2, 20);		        
		memset(barcode + 3, '-', 1);
		memmove(barcode + 6, barcode + 5, 20);
		memset(barcode + 6, '-', 1);
		memmove(barcode + 11, barcode + 10, 20);
		memset(barcode + 11, '-', 1);
		memmove(barcode + 15, barcode +14, 20);
		memset(barcode + 15, '-', 1);
	}
            
    // Determine country    
    if (countryCode == 850){									
        char country[50] = {"Cuba"};
    } else if (countryCode == 858){
        char country[50] ={"Slovakia"};
    } else if (countryCode == 859){
        char country[50] ={"Czech Republic"};
    } else if (countryCode == 860){
        char country[50] ={"Serbia"};
    } else if (countryCode == 865){
        char country[50] ={"Mongolia"};
    } else if (countryCode == 867){
        char country[50] ={"North Korea"};
    } else if (countryCode >= 868 && countryCode <= 869){
        char country[50] ={"Turkey"};
    } else if (countryCode >= 870 && countryCode <= 879){
        char country[50] ={"Netherlands"};
    } else if (countryCode == 880){
        char country[50] ={"South Korea"};
    } else if (countryCode == 884){
        char country[50] ={"Cambodia"};
    } else if (countryCode == 885){
        char country[50] ={"Thailand"};
    } else if (countryCode == 888){
        char country[50] ={"Singapore"};
    } else if (countryCode == 890){
        char country[50] ={"India"};
    } else if (countryCode == 893){
        char country[50] ={"Vietnam"};
    } else if (countryCode == 896){
        char country[50] ={"Pakistan"};
    } else if (countryCode == 899){
        char country[50] ={"Indonesia"};
    } else if (countryCode >= 900 && countryCode <= 919){
        char country[50] ={"Austria"};
    } else if (countryCode >= 930 && countryCode <= 939){
        char country[50] ={"Australia"};
    } else if (countryCode >= 940 && countryCode <= 949){
        char country[50] ={"New Zealand"};
    } else if (countryCode == 950){
        char country[50] ={"Global Office"};
    } else if (countryCode == 955){
        char country[50] ={"Malaysia"}; 
    } else if (countryCode == 958){
        char country[50] ={"Macau"};
    } else if (countryCode >= 977 && countryCode <= 979){
        char country[50] ={"ISBN"};
    } else {
        char country[50] ={"country no recognised"}; 
    }    
        
        
    // Print Information
    // ISBN   
    if (countryCode > 976 && countryCode < 979){

        printf("Barcode is %s%d\n\n", barcode, checksum);

        printf("Barcode is a valid EAN code\n\n");

        printf("Country code: %ld (%s)\n", countryCode, country);

        printf("Language Group: %lld\n", language);

        printf("Publisher: %lld\n", publish);

        printf("Item: %lld\n", title);

        printf("Checksum: %d\n\n", checksum);

        printf("Checksum calculation:\n");
        printf("%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d\n",
            dig0, dig1, dig2, dig3, dig4, dig5, dig6,
            dig7, dig8, dig9, dig10, dig11, checksum);
        printf("%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d  =  %ld %% 10 = %ld; (10 - %ld) %% 10 = %d\n", 
            dig0, dig1*3, dig2, dig3*3, dig4, dig5*3, dig6, dig7*3,
            dig8, dig9*3, dig10, dig11*3, sum, rem, rem, checksum);
    // EAN        
    } else {

        printf("Barcode is %s%d\n\n", barcode, checksum);

        printf("Barcode is a valid EAN code\n\n");

        printf("Country code: %ld (%s)\n", countryCode, country);

        printf("Manufacturer code: %lld\n", manufac);

        printf("Product code: %lld\n", product);

        printf("Checksum: %d\n\n", checksum);

        printf("Checksum calculation:\n");
        printf("%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d\n",
            dig0, dig1, dig2, dig3, dig4, dig5, dig6,
            dig7, dig8, dig9, dig10, dig11, checksum);
        printf("%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d%3d  =  %ld %% 10 = %ld; (10 - %ld) %% 10 = %d\n", 
                    dig0, dig1*3, dig2, dig3*3, dig4, dig5*3, dig6, dig7*3,
                    dig8, dig9*3, dig10, dig11*3, sum, rem, rem, checksum);                
    }
    return 0;
    }

First let me say sorry about the massive amounts of code but I feel it's essential and I've tried to make it neat and readable. so this is meant to be a code for reading either a 13 digit or 12 digit bar code. then calculating each part of the bar code and printing out some information. problem is its not working. it is printing out random junk. if anyone could see anything i might have missed in the process it would be much appreciated


many thanks

2
Contributors
1
Reply
3
Views
5 Years
Discussion Span
Last Post by gusano79
0

if anyone could see anything i might have missed in the process it would be much appreciated

I copied your code in, and it builds, but I also got 37 warnings, for example, "warning: unknown conversion type character 'l' in format" Are you getting these as well?

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.