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

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

Edited 5 Years Ago by Prankmore: n/a

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);

;)

This question has already been answered. Start a new discussion instead.