Holy moly. Might I recommend reading up on variable scope and lifetime?
Narue
Bad Cop
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
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
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
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