简体   繁体   中英

C Programming: Creating a Program to Pull Latitude and Longitude from a typedef struct

So I have a project for my class where I need to write a program where the User will type in a City then the program will look up this city from a typedef structure then print that said city's latitude and longitude coordinates. My idea is to take the cities name as an input then somehow compare the city name with the ones in the structure (pointers maybe?) then get the latitude and the longitude when the city matches. I think that I will need to create a loop program to read the data until I find a match. Can you tell me if I am on the right track, or how you would go about writing the program? I attached a small portion of the structure (full one has 200 cities) if someone wants to try it out.

  #define MAX_CITY_LEN   35

   typedef struct { 
            char  name[MAX_CITY_LEN];
            float latitude;
            float longitude;
           } PLACE_T;

   PLACE_T places[] =
     {{"Aberdeen,Scotland",57.15,-2.15},
      {"Adelaide,Australia",-34.92,138.6},
      {"Albany,NY,USA",42.67,-73.75},
      {"Albuquerque,NM,USA",35.08,-106.65},
      {"Algiers,Algeria",36.83,3.0},
      {"Amarillo,TX,USA",35.18,-101.83},

Thanks, Alex

do like this:

 typedef struct { 
            char  name[MAX_CITY_LEN];
            float latitude;
            float longitude;
 } PLACE_T;

 PLACE_T places[] ={
      {"India,kochi",57.15,-2.15},
      {"Adelaide,Australia",-34.92,138.6},
      {"Albany,NY,USA",42.67,-73.75},
      {"Albuquerque,NM,USA",35.08,-106.65},
      {"Algiers,Algeria",36.83,3.0},
      {"Amarillo,TX,USA",35.18,-101.83},
   };

int main(){
   int j;
  int found=0;
  char city[MAX_CITY_LEN];
  printf("enter city name:");
  fgets(city,MAX_CITY_LEN,stdin);
  city[strlen(city)-1]='\0';
  int citySize=(sizeof(places)/sizeof(places[0]));
  for(j=0;j<citySize;j++){
     if(strcmp(city,places[j].name)==0){
        found=1;                                
        printf("lat = %f, long = %f",places[j].latitude,places[j].longitude); 
        break;                              
     }
   }
   if(!found)
     printf("City not found");

    return 0;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM