I'm trying to determine then print the country of origin of a product based on its bar-code.
I have isolated that part of the bar-code and stored it as countryCode, now what i am trying to do is determine its country then store the country name as a string so i can print it out at the end with other details of the product. right now i am getting random character that make no sense

My code for storing the country name is

// 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"};

my print statement is

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

which should print as

Country code: 955 (Macau)

thanks

Dani AI

Generated

Short version: this is a scope/lifetime problem. As pointed out, the arrays you create inside each if are local to that block and go away when the block ends. Printf later therefore sees no valid string and you get garbage. Declare the storage in an outer scope (or return a pointer to a static string) and then print.

A simple, safe pattern using string literals:

const char *country = "Unknown";

if (countryCode == 1) {
  country = "CountryA";
} else if (countryCode >= 10 && countryCode <= 19) {
  country = "RegionB";
}

/* later, print with the correct format for countryCode */
printf("Country code: %d (%s)\n", (int)countryCode, country);

If you need to modify the name at runtime (not a literal), allocate a buffer once and copy into it safely:

char country_buf[64] = "Unknown";
/* use snprintf or strncpy, never unchecked strcpy */
snprintf(country_buf, sizeof country_buf, "%s", "SomeName");
printf("%d (%s)\n", (int)countryCode, country_buf);

For maintainability use a table-driven lookup (agree with ): keep an array of ranges and names or a small country_from_code() function that returns const char *. Extra tips: match printf format specifiers to the variable type (%d for int, %ld for long), compile with warnings (-Wall -Wextra), and mark literal pointers const char * so you don’t accidentally try to modify them. These changes will stop the “random characters” and make the mapping easier to manage.

Recommended Answers

All 6 Replies

Holy moly. Might I recommend reading up on variable scope and lifetime?

but i thought that because each variable is being declared inside an if statement then it shouldn't be a problem. also the variable can't be declared twice since only 1 if statement can be true

is that what you are saying. or should i declare country as a string first then set country = "countryname"? i think i tried that before and it didn't work. but i will try again now

but i thought that because each variable is being declared inside an if statement then it shouldn't be a problem

And after the body of the if ends, the array is destroyed. The code you posted does nothing but create an array, initialize it, and then destroy it.

i see thankyou. i will initialise it outside then. thankyou very much

Try this:

//Before main() define a struct with 
struct countries {
  char name[50];
  int numNation;
}

//now make an array of these structs, in main() 
struct countries nations[500?];  
char myNation[50];

//and read in your data from a file, into the array of structs.

//Now a for loop can replace all the if else switch statements:

for(i=0;i<500?;i++) {
  if(nations[i].numNation = numBarcode) {
    strcopy(myNation, nations[i].name); //or print the country name here
    break;
  }
}

printf("%s\n", myNation);

;)

thanks, problem solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.