Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to compare things in a struct?

Status
Not open for further replies.

dgboult

Programmer
Apr 29, 2001
7
US
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[] = {{&quot;Argentina&quot;, 54},{&quot;Brazil&quot;, 55},{&quot;Colombia&quot;, 57},{&quot;Ethiopia&quot;, 251}};

char c;

printf(&quot;Enter a country you want the dialing code for: &quot;);
scanf(&quot;%c&quot;,&c);

printf(&quot;%d\n&quot;, country_codes[0].code);

return 0;
}

Thanks for helping!!
 
Just compare members of the struct. If there is a string, use strcmp. John Fill
1c.bmp


ivfmd@mail.md
 
Hi!!!

Lets first check what is the error in ur code -

C ...
u just accepted the character, I suppose the first character of the country, but hey where is an attempt to compare it with the first character of member variable 'country' of your struct 'country_codes'

//After accpeting the character
//convert it to upper case as your country names are in
//uppercase, thereafter -
if(c>=97 && c<=122)
c-=32; // to convert lowercase to upper.
for(i=0;i<4;i++)
{
if(c == country_codes.country[0])
printf(&quot;The code is %d&quot;,country_codes.code);
}

Check it out ....
I hope it helps,
Regards,
SwapSawe.
 
>if(c>=97 && c<=122)
> c-=32; // to convert lowercase to upper.

You'll want to use the toupper() functions instead of relying on the ASCII equivalents for these kinds of operations:

c=toupper((unsigned char)c);

It's no big deal if c is already upper case. If you did want to do the comparison, you'll want:

if (c>='a' && c<='z')
c=toupper((unsigned char)c);

Don't forget to #include <ctype.h>

This whole comparison/to upper thing isn't what's needed here though. You want the numeric dialing code from the user, yet you're only reading in one character from stdin. What you really have to do is get the user's input in the form of a string and then use strtol() to convert the string to a number, casting to the appropriate type.

#include <stdio.h>
#include <stdlib.h>

/* ... */

char tmp[50];
char c;
const struct dialing_code country_codes[] = {{&quot;Argentina&quot;, 54},{&quot;Brazil&quot;, 55},{&quot;Colombia&quot;, 57},{&quot;Ethiopia&quot;, 251}};


/* Get a line from the user */
if (fgets(tmp,sizeof tmp,stdin)!=NULL) {
int i;
c=(char)strtol(tmp,NULL,10); /* convert input */

/* Find the country */
for (i=0;i<(int)(sizeof country_codes/sizeof country_codes[0]);++i) {
if (country_codes.code==c) {
printf(&quot;Country is %s\n&quot;,country_codes.country);
break;
}
}
}

Of course, 50 is an arbitrary number for a string length, though certainly long enough in this case.

You may want to consider using type int instead of type char for your dialing code field, though what you're doing isn't wrong and maybe necessary if you're constrained by unusual memory requirements.

One problem though is that your dialing code for Ethiopia isn't guaranteed to fall into the range of char. A variable of type char without a signed or unsigned specifier could be either signed or unsigned -- it's implementation-defined. To force the unsignedness and guarantee that the dialing code can fit, change your dialing code field to type unsigned char. Or, just use int or short.

Russ
bobbitts@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top