954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

storing a string to print later

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

Prankmore
Newbie Poster
14 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

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

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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

Prankmore
Newbie Poster
14 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 
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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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

Prankmore
Newbie Poster
14 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

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


;)

Adak
Nearly a Posting Virtuoso
1,479 posts since Jun 2008
Reputation Points: 425
Solved Threads: 185
 

thanks, problem solved

Prankmore
Newbie Poster
14 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: