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!

compare things in struct 2

Status
Not open for further replies.

dgboult

Programmer
Apr 29, 2001
7
US
Thanks, for all that help on the first one, but I ran into another problem. The code will only read the first letter of the country and compare that. This is a problem because some of the country's start with the same first letter. Wright now all I have to do is enter a capital letter that one of the country's begen with and it will give its code. How do you compare the whole word?

#include <stdio.h>

struct dialing_code {
char country[19]; /*would changing this to a pointer be
int code; beter *country ? */
};

int main(void)
{
const struct dialing_code country_codes[] = {{&quot;Argentina&quot;, 54},{&quot;Bangladesh&quot;, 880},{&quot;Brazil&quot;, 55},{&quot;China&quot;, 86},{&quot;Colombia&quot;, 57},{&quot;Ethiopia&quot;, 251}};

int i;
char c;

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

if(c>=97 && c<=122)
c-=32;

for(i=0;i<6;i++)
{
if(c== country_codes.country[0])
printf(&quot;%d\n&quot;, country_codes.code);

}

return 0;
}

Thanks again for your help!!
 
Well,
I don't want the user to only enter one letter, but even when I tried using getchar() or storing it in a array I still got the same answer. Would using one of these be better?

printf(&quot;Enter a country you want the dialing code for: &quot;);
ch = getchar();

printf(&quot;Enter a country you want the dialing code for: &quot;);
for(i=0;i<19;i++)
scanf(&quot;%c&quot;, &a);



 
You have to read in the string that user specifies. Here is some code for you:

#include <stdio.h>
#include <string.h>

typedef struct dialing_code {
char country[19];
int code;
};

int main(void)
{
const struct dialing_code country_codes[] = {{&quot;Argentina&quot;,
54},{&quot;Bangladesh&quot;, 880},{&quot;Brazil&quot;, 55},{&quot;China&quot;, 86},{&quot;Colombia&quot;,
57},{&quot;Ethiopia&quot;, 251}};

int i;
char s[20];

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

if(s[0] >= 97 && s[0] <= 122)
s[0] -= 32;

for(i=0;i<6;i++)
{
if( strcmp(s,country_codes.country) == 0)
printf(&quot;%d\n&quot;, country_codes.code);

}

return 0;
}

...
strcmp() returns 0 if two compared strings are equal.
Best Regards,

aphrodita@mail.krovatka.ru {uiuc rules}
 
>int i;
>char s[20];

>printf(&quot;Enter a country you want the dialing code for: &quot;);

fflush(stdout);

To make sure the user sees the prompt before they can enter the string.

>scanf(&quot;%s&quot;, &s);

scanf(&quot;%s&quot;,s);

This is as bad as using gets() though because you don't limit the # of characters that can be entered by the user.

scanf(&quot;%19s&quot;,s);

Better though:

if (fgets(s,sizeof s,stdin)!=NULL) {
/* ... */

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

Part and Inventory Search

Sponsor

Back
Top