Thread: Why is my program not working

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    20

    Why is my program not working

    i want to write a simple country code program. the program will ask the user to enter a country code. if the country code number matches to the struct then it gives out a result
    if the user enters a country code which is not listed in the structure then an error message shows

    when i write the correct country code the error message shows up. can someone help tell me where i have made a mistake in my code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    
    
    
    struct dialing_code {
    
    
        char *country;
        int code;
    
    
    
    
    };
    
    
    const struct dialing_code country_codes[]=
    {   {"Argentina",          54}, {"Bangladesh",             880},
        {"Brazil",             55}, {"Colombia",                57},
    
    
    
    
    };
    
    
    
    
    int main (void)
    
    
    {
        int code, i;
        printf("Enter countrycode: ");
        scanf("%d", &code);
        if (country_codes[i].code==code) {
        for (i=0; i<sizeof(country_codes)/sizeof(country_codes[0]); i++) {
           
                printf("Country code %d is for %s", code, country_codes[i].country);
                printf("\n");
            
        }
        }
        else printf("Error\n");
        
        return 0;
    
    
    }

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,136
    You've got your loop and if statement inside-out. The if should be inside the loop so that it is tested for every i. And it's probably better to just set a flag and break the loop if you found the code, then have another if statement test the flag.

    BTW, you should remove all the extra blank lines from your code when you post.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct dialing_code {
        const char *country;
        int code;
    };
    
    const struct dialing_code country_codes[] = {
        {"Argentina",          54}, {"Bangladesh",             880},
        {"Brazil",             55}, {"Colombia",                57},
    };
     
    int main (void) {
        int size = sizeof country_codes / sizeof country_codes[0];
    
        int code = 0;
        printf("Enter countrycode: ");
        scanf("%d", &code);
    
        int found = 0, i = 0;
        for (i = 0; i < size; i++) {
            if (country_codes[i].code == code) {
                found = 1;
                break;
            }
        }
    
        if (found)
            printf("Country code %d is for %s\n", code, country_codes[i].country);
        else
            printf("Code not found\n");
    
        return 0;
    }
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-25-2016, 08:49 AM
  2. Need help as to why the program is not working
    By Jesse Bettcher in forum C++ Programming
    Replies: 2
    Last Post: 10-29-2014, 03:12 PM
  3. Why is my program not working?
    By Sourabh Verma in forum C Programming
    Replies: 5
    Last Post: 02-19-2012, 11:19 AM
  4. Why is my program not working ?
    By swansea in forum C++ Programming
    Replies: 2
    Last Post: 02-28-2010, 04:40 PM
  5. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM

Tags for this Thread