Thread: if...else ERROR

  1. #1
    Registered User
    Join Date
    May 2021
    Posts
    3

    Question if...else ERROR

    Can i ask how to debug the error?
    [Error] incompatible types in assignment of 'char' to 'char [30]'
    Code:
    #include <stdio.h> 
    struct record
    {
        char name[30], lat_dir[30], lon_dir[30];//declare city name as name
        int pop;                                 //declare population as pop
        int lat_deg, lat_min, lon_deg, lon_min;
        float lat_sec, lon_sec; 
    };
    
    
    void display(struct record city);
    
    
    int main()
    {    
        struct record city;    
        
        printf("\nPlease enter city name : ");
        scanf("%[^\n]", &city.name);
        
        printf("\nPlease enter population : ");
        scanf("%d", &city.pop);
        
        printf("\nFor location...\n");
        
        printf("Please enter latitude of city in degrees, minutes, seconds and its cardinal direction(N/S) : \n");
        scanf("%d %d %f", &city.lat_deg, &city.lat_min, &city.lat_sec);
        if(city.lat_deg >= 0 && city.lat_deg <= 90)
            city.lat_dir = 'N';
        else if (city.lat_deg >= -90 && city.lat_deg < 0)
            city.lat_dir = 'S';
        else
            city.lat_dir = 'INVALID!';
            
        printf("Please enter longitude of city in degrees, minutes, seconds and its cardinal direction(E/W) : \n");
        scanf("%d %d %f", &city.lon_deg, &city.lon_min, &city.lon_sec);    
        if(city.lon_deg >= 0 && city.lon_deg <= 180)
            city.lon_dir = 'E';
        else if (city.lon_deg >= -180 && city.lon_deg < 0)
            city.lon_dir = 'W';
        else
            city.lon_dir = 'INVALID';
                
        display(city);    
    }
    
    void display(struct record city)
    {
    	printf("\n\n----------------------");
            printf("\nDisplaying information");
            printf("\n----------------------");
    	printf("\nCity name : %s", city.name);
    	printf("\nPopulation : %d", city.pop);
    	printf("\nLocation : %d%c\%d\'%f\" %s", city.lat_deg, 0xf8, city.lat_min, city.lat_sec, city.lat_dir);
    	printf("\t   %d%c\%d\'%f\" %s", city.lon_deg, 0xf8, city.lon_min, city.lon_sec, city.lon_dir);
    }
    Last edited by hij; 05-09-2021 at 07:26 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > city.lat_dir = 'INVALID!';
    Use strcpy to copy strings to arrays.

    strcpy(city.lat_dir,"INVALID");
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338

    Post

    Code:
    #include <stdio.h> 
    #include <string.h>
    
    struct record
    {
        char name[30], lat_dir[30], lon_dir[30];//declare city name as name
        int pop;                                 //declare population as pop
        int lat_deg, lat_min, lon_deg, lon_min;
        float lat_sec, lon_sec; 
    };
    
    void display(struct record city);
    
    int main()
    {    
        struct record city;    
        
        printf("\nPlease enter city name : ");
        //scanf("%[^\n]", &city.name);
        scanf("%[^\n]", city.name);
        
        printf("\nPlease enter population : ");
        scanf("%d", &city.pop);
        
        printf("\nFor location...\n");
        
        printf("Please enter latitude of city in degrees, minutes, seconds and its cardinal direction(N/S) : \n");
        scanf("%d %d %f", &city.lat_deg, &city.lat_min, &city.lat_sec);
        if(city.lat_deg >= 0 && city.lat_deg <= 90)
            //city.lat_dir = 'N';
            strcpy(city.lat_dir,"N");
        else if (city.lat_deg >= -90 && city.lat_deg < 0)
            //city.lat_dir = 'S';
            strcpy(city.lat_dir,"S");
        else
            //city.lat_dir = 'INVALID!';
            strcpy(city.lat_dir,"INVALID!");
            
        printf("Please enter longitude of city in degrees, minutes, seconds and its cardinal direction(E/W) : \n");
        scanf("%d %d %f", &city.lon_deg, &city.lon_min, &city.lon_sec);    
        if(city.lon_deg >= 0 && city.lon_deg <= 180)
            //city.lon_dir = 'E';
            strcpy(city.lon_dir,"E");
        else if (city.lon_deg >= -180 && city.lon_deg < 0)
            //city.lon_dir = 'W';
            strcpy(city.lon_dir,"W");
        else
            //city.lon_dir = 'INVALID';
            strcpy(city.lon_dir,"INVALID!");
                
        display(city);    
    }
    
    void display(struct record city)
    {
        printf("\n\n----------------------");
            printf("\nDisplaying information");
            printf("\n----------------------");
        printf("\nCity name : %s", city.name);
        printf("\nPopulation : %d", city.pop);
        printf("\nLocation : %d%c\%d\'%f\" %s", city.lat_deg, 0xf8, city.lat_min, city.lat_sec, city.lat_dir);
        printf("\t   %d%c\%d\'%f\" %s", city.lon_deg, 0xf8, city.lon_min, city.lon_sec, city.lon_dir);
        printf("\n");
    }
    "without goto we would be wtf'd"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compile error? Logic error? Syntax Error? Please help
    By Khody Afkhami in forum C Programming
    Replies: 4
    Last Post: 10-11-2014, 01:36 AM
  2. Replies: 6
    Last Post: 10-29-2012, 03:33 AM
  3. Replies: 4
    Last Post: 07-24-2011, 09:38 PM
  4. Replies: 1
    Last Post: 11-15-2010, 11:14 AM
  5. Replies: 3
    Last Post: 10-02-2007, 09:12 PM

Tags for this Thread