Thread: Error in Compiling a C program in MSVS 2010 : 1 unresolved externals.

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    7

    Error in Compiling a C program in MSVS 2010 : 1 unresolved externals.

    Now i know, i need to define a function that is why this error occurs but i have checked this code many times and it still gives me the error.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    
    
    void load_menu(void);
    void Create_inv(void);
    void Calc_Sales(void);
    void Update_inv(void);
    
    
    int main(int argc, char** argv)
    {
        
        load_menu();
        return 0;
    }
    
    
     
    void load_menu(void)
    {
        int choice;
     
        do
        {
            printf("==================================\n\nMenu\n==================================");
            printf("\n 1.Create an Inventory \n");
            printf("\n 2. Calculate Sales\n");
            printf("\n3. Update an Inventory\n");
            printf("\n 4. Exit\n");
            scanf("%d",&choice);
     
            switch(choice)
            {
                case 1: Create_inv();
                    break;
                case 2: Calc_Sales();
                    break;
                case 3: Update_inv();
                    break;
                case 4: printf("Quitting program!\n");
                    exit(0);
                    break;
                default: printf("Invalid choice!\n");
                    break;
            }
     
        } while (choice != 4);
     
    }
     
    void Create_inv(void)
    {
        struct date
    {
        int day;
        int month;
        int year;
    };
    
    
    struct details
    {
        char name[50];
        int price;
        int code;
        int qty;
        struct date mfg;
    };
    
    
    
    
        struct details item[50];
    int n,i;
    int ch;
    
    
    printf("Enter number of items:");
    scanf("%d",&n);
    
    
    
    
    for(i=0;i<n;i++)
    {
        getchar();
        printf("Item name:");
        scanf("%[^\n]",item[i].name);
    
    
    
    
        printf("Item code:");
        scanf("%d",&item[i].code);
    
    
    
    
        printf("Quantity:");
        scanf("%d",&item[i].qty);
    
    
    
    
        printf("price:");
        scanf("%d",&item[i].price);
    
    
    
    
        printf("Manufacturing date(dd-mm-yyyy):");
        scanf("%d-%d-%d",&item[i].mfg.day,&item[i].mfg.month,&item[i].mfg.year);
         /* Flushes input buffer from the newline from scanf() */
        while ( (ch = getchar()) != '\n' && ch != EOF) ;
     
        printf("\n\nPress ENTER to continue.");
        while ( (ch = getchar()) != '\n' && ch != EOF);
        getch();}
    
    
    
    
    
    
    
    
    {
        FILE *fptr;
    fptr=(fopen("draftday.txt","a"));
    if(fptr==NULL){
       printf("Error!");
       exit(1);}
    
    
    {fprintf(fptr,"             *****  INVENTORY *****\n\n\n"
                 "------------------------------------------------------------------\n\n"
                 "S.N.|    NAME           |   CODE   |  QUANTITY |  PRICE  |MFG.DATE\n\n"
                 "------------------------------------------------------------------\n\n");
    
    
    
    
    for(i=0;i<n;i++)
                fprintf(fptr, "%d     %-15s        %-d          %-5d     %-5d     %d/%d/%d\n",i+1,item[i].name,item[i].code,item[i].qty,item[i].price,
                  item[i].mfg.day,item[i].mfg.month,item[i].mfg.year);
    
    
    fclose(fptr);
     getch();
    }   }  }
     
    void Calc_Sales(void)
    
    
        {char o;
        int ch;
        float num1,num2;
        printf("Enter operator either + or - or * or divide : ");
        scanf("%c",&o);
        printf("Enter two operands: ");
        scanf("%f%f",&num1,&num2);
        switch(o) {
            case '+':
                printf("%.1f + %.1f = %.1f",num1, num2, num1+num2);
                break;
            case '-':
                printf("%.1f - %.1f = %.1f",num1, num2, num1-num2);
                break;
            case '*':
                printf("%.1f * %.1f = %.1f",num1, num2, num1*num2);
                break;
            case '/':
                printf("%.1f / %.1f = %.1f",num1, num2, num1/num2);
                break;
            default:
                /* If operator is other than +, -, * or /, error message is shown */
                printf("Error! operator is not correct");}
                
        
    
    
        /* Flushes input buffer */
        while ((ch = getchar()) != '\n' && ch != EOF) ;
     
        printf("\n\nPress ENTER to continue.");
        while ((ch = getchar()) != '\n' && ch != EOF);
     
        getch();
    }

  2. #2
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Seems like you have not implemented your 'update_inv' function.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    Your question has been answered, but allow me to make some additional observations.

    Do yourself a favor and make sure you format your code well. This means consistent indendation, brace placement, and vertical space.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    
    void load_menu(void);
    void Create_inv(void);
    void Calc_Sales(void);
    void Update_inv(void);
    
    int main(int argc, char** argv)
    {
        load_menu();
        return 0;
    }
    
    
    void load_menu(void)
    {
        int choice;
    
        do
        {
            printf("==================================\n\nMenu\n==================================");
            printf("\n 1.Create an Inventory \n");
            printf("\n 2. Calculate Sales\n");
            printf("\n3. Update an Inventory\n");
            printf("\n 4. Exit\n");
            scanf("%d",&choice);
    
            switch(choice)
            {
                case 1: Create_inv();
                    break;
                case 2: Calc_Sales();
                    break;
                case 3: Update_inv();
                    break;
                case 4: printf("Quitting program!\n");
                    exit(0);
                    break;
                default: printf("Invalid choice!\n");
                    break;
            }
            
        } while (choice != 4);
    }
    
    void Create_inv(void)
    {
        struct date
        {
            int day;
            int month;
            int year;
        };
    
        struct details
        {
            char name[50];
            int price;
            int code;
            int qty;
            struct date mfg;
        };
    
        struct details item[50];
        int n,i;
        int ch;
    
        printf("Enter number of items:");
        scanf("%d",&n);
    
        for(i=0;i<n;i++)
        {
            getchar();
            printf("Item name:");
            scanf("%[^\n]",item[i].name);
    
            printf("Item code:");
            scanf("%d",&item[i].code);
    
            printf("Quantity:");
            scanf("%d",&item[i].qty);
    
            printf("price:");
            scanf("%d",&item[i].price);
    
            printf("Manufacturing date(dd-mm-yyyy):");
            scanf("%d-%d-%d",&item[i].mfg.day,&item[i].mfg.month,&item[i].mfg.year);
         
            /* Flushes input buffer from the newline from scanf() */
            while ( (ch = getchar()) != '\n' && ch != EOF) ;
            printf("\n\nPress ENTER to continue.");
            while ( (ch = getchar()) != '\n' && ch != EOF);
            getch();
        }
    
        {
            FILE *fptr;
    
            fptr=(fopen("draftday.txt","a"));
            if(fptr==NULL){
                printf("Error!");
                exit(1);
            }
    
            {
                fprintf(fptr,"             *****  INVENTORY *****\n\n\n"
                 "------------------------------------------------------------------\n\n"
                 "S.N.|    NAME           |   CODE   |  QUANTITY |  PRICE  |MFG.DATE\n\n"
                 "------------------------------------------------------------------\n\n");
    
                for(i=0;i<n;i++)
                    fprintf(fptr, "%d     %-15s        %-d          %-5d     %-5d     %d/%d/%d\n",i+1,item[i].name,item[i].code,item[i].qty,item[i].price,
                        item[i].mfg.day,item[i].mfg.month,item[i].mfg.year);
    
                fclose(fptr);
                getch();
            }
        }
    }
    
    void Calc_Sales(void)
    {
        char o;
        int ch;
        float num1,num2;
    
        printf("Enter operator either + or - or * or divide : ");
        scanf("%c",&o);
        printf("Enter two operands: ");
        scanf("%f%f",&num1,&num2);
    
        switch(o) {
            case '+':
                printf("%.1f + %.1f = %.1f",num1, num2, num1+num2);
                break;
            case '-':
                printf("%.1f - %.1f = %.1f",num1, num2, num1-num2);
                break;
            case '*':
                printf("%.1f * %.1f = %.1f",num1, num2, num1*num2);
                break;
            case '/':
                printf("%.1f / %.1f = %.1f",num1, num2, num1/num2);
                break;
            default:
                /* If operator is other than +, -, * or /, error message is shown */
                printf("Error! operator is not correct");}
    
        /* Flushes input buffer */
        while ((ch = getchar()) != '\n' && ch != EOF) ;
        printf("\n\nPress ENTER to continue.");
        while ((ch = getchar()) != '\n' && ch != EOF);
        getch();
    }
    In your "Create_inv()" function, you have a few random blocks of code (i.e. code within braces - see lines 98-120 and 107-119 in the version I posted). While this is technically not incorrect, it serves no purpose since that code will execute anyway, and can cause confusion.

    Also in that function, you declare an array of struct details with 50 elements. Then you receive user input into "n" and iterate through the array "n" times. What happens if the user enters a value greater than 50? I'd suggest you #define the size of the arrray and use this constant in lieu of the magic number 50 ... and also check that the user input does not exceed the size of the array. (Note that you should avoid magic numbers altogether - named constants make the program more readable and easier to modify.)

    You should declare your structs at the top of your file, instead of locally in "Create_inv()", since presumably you'll need to use this struct in other functions (e.g. the missing "Update_inv()" function).

    "scanf()" is not the best way to read strings from the user. Consider something better, such as "fgets()": FAQ > Get a line of text from the user/keyboard (C) - Cprogramming.com

    I've already went over issues with the "Calc_Sales()" function in your other thread.

    You might want to spend more time planning out your program before you start writing it.
    Last edited by Matticus; 12-23-2014 at 06:02 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unresolved externals(??)
    By PurpleChershire in forum C++ Programming
    Replies: 8
    Last Post: 01-08-2014, 10:05 AM
  2. Replies: 7
    Last Post: 10-27-2012, 02:55 PM
  3. error LNK1120: 1 unresolved externals
    By ncode in forum C Programming
    Replies: 2
    Last Post: 08-01-2011, 11:21 PM
  4. fatal error LNK1120: 2 unresolved externals
    By SgtPooki in forum C++ Programming
    Replies: 10
    Last Post: 08-09-2010, 07:28 AM
  5. Fatal Error LNK1220: 1 unresolved externals
    By zenghoong in forum C Programming
    Replies: 3
    Last Post: 10-12-2009, 03:24 AM

Tags for this Thread