Hello,
My question is how would I be able to compare what the user enters to what is in the struct? I want the program to give the corresponding dialing code to a country that the user enters, and if it is not there display a error message.Example-Colombia dialing code is 57.I've tried using loops and comparing c with country_codes[], but I either get a error or it will only display the first country's dialing code.This is what I have so far and all it does is print the first country's dialing code. I know that it is because in the printf statment that country_codes has a [0] in it meaning to print the first one in the array but when I change it to country_codes[] it gives me a error message. Please help!!
#include <stdio.h>
struct dialing_code {
char country[19];
int code;
};
int main(void)
{
const struct dialing_code country_codes[] = {{"Argentina", 54},{"Brazil", 55},{"Colombia", 57},{"Ethiopia", 251}};
char c;
printf("Enter a country you want the dialing code for: "
scanf("%c",&c);
printf("%d\n", country_codes[0].code);
return 0;
}
Thanks for helping!!
My question is how would I be able to compare what the user enters to what is in the struct? I want the program to give the corresponding dialing code to a country that the user enters, and if it is not there display a error message.Example-Colombia dialing code is 57.I've tried using loops and comparing c with country_codes[], but I either get a error or it will only display the first country's dialing code.This is what I have so far and all it does is print the first country's dialing code. I know that it is because in the printf statment that country_codes has a [0] in it meaning to print the first one in the array but when I change it to country_codes[] it gives me a error message. Please help!!
#include <stdio.h>
struct dialing_code {
char country[19];
int code;
};
int main(void)
{
const struct dialing_code country_codes[] = {{"Argentina", 54},{"Brazil", 55},{"Colombia", 57},{"Ethiopia", 251}};
char c;
printf("Enter a country you want the dialing code for: "
scanf("%c",&c);
printf("%d\n", country_codes[0].code);
return 0;
}
Thanks for helping!!