Thread: Silly error

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    114

    Silly error

    Ok im writing a little program to get practice, its going to be an email whitelist thing, you can either add a email to the list or check if a email is in the list. Anyway im testing out what ive written so far and im getting an errror " [Linker error] undefined reference to `addemail' ". Cant see what ive done wrong anyway, heres my code.

    Code:
    #include <stdio.h>
    
    void addemail();
    
    main()
    {
        int choice;
        
        printf("Email whitelist");
        printf("(a) to add email, (c) to check email against list, (e) to exit.\n");
        printf("Choice: ");
        choice = -1;
        while (choice == -1)
        {
            choice=getchar();
        }
        switch (choice)
        {
            case 'a':
                addemail();
                break;
            case 'c':
               /* checkemail(); */
                break;
            case 'e':
                break;
            default:
                printf("Invalid Choice.");
                break;
        }    
        
        void addemail();
        {
            char buf[40];
            FILE *fp;
            
            printf("Enter email: ");
            fgets(buf, sizeof(buf), stdin);
            
            fp = fopen("emails","a+");
            fputs(buf,fp);
            fclose(fp);
            
            printf("\nEmail address %s added.",buf);
        }    
    }

  2. #2
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    You are defining your addemail function inside of your main function. ISO C doesn't support nested function and your compiler should say that if you have all warnings turned on. You have to end your main function before defining your addemail function. Also, use int main() and return an exit status
    Code:
    #include <stdio.h>
    
    void addemail();
    
    int main()
    {
        int choice;
        
        printf("Email whitelist");
        printf("(a) to add email, (c) to check email against list, (e) to exit.\n");
        printf("Choice: ");
        choice = -1;
        while (choice == -1)
        {
            choice=getchar();
        }
        switch (choice)
        {
            case 'a':
                addemail();
                break;
            case 'c':
               /* checkemail(); */
                break;
            case 'e':
                break;
            default:
                printf("Invalid Choice.");
                break;
        }
        return 0;
    }
        
    void addemail()
        {
            char buf[40];
            FILE *fp;
            
            printf("Enter email: ");
            fgets(buf, sizeof(buf), stdin);
            
            fp = fopen("emails","a+");
            fputs(buf,fp);
            fclose(fp);
            
            printf("\nEmail address %s added.",buf);
    }
    You can also check the return values of the functions you use, such as fopen, fgets, etc. We have a faq that might have some useful entries to you: http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
    Last edited by chrismiceli; 10-16-2004 at 04:19 PM.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    1. C does not support nested functions
    2. You haven't written a function called addemail(), though you have managed to prototype it TWICE (check the semicolons)
    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.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Functions cannot be defined inside of other functions. Take your addemail function out of main and paste it after main (remembering to remove the semicolon).
    Code:
    #include <stdio.h>
    
    void addemail(void);
    
    int main(void)
    {
        int choice;
        
        printf("Email whitelist");
        printf("(a) to add email, (c) to check email against list, (e) to exit.\n");
        printf("Choice: ");
        choice = -1;
        while (choice == -1)
        {
            choice=getchar();
        }
        switch (choice)
        {
            case 'a':
                addemail();
                break;
            case 'c':
               /* checkemail(); */
                break;
            case 'e':
                break;
            default:
                printf("Invalid Choice.");
                break;
        }
    
        return 0;
    }  
    
    void addemail(void)
    {
        char buf[40];
        FILE *fp;
            
        printf("Enter email: ");
        fgets(buf, sizeof(buf), stdin);
            
        fp = fopen("emails","a+");
        fputs(buf,fp);
        fclose(fp);
            
        printf("\nEmail address %s added.",buf);
    }
    >while (choice == -1)
    EOF isn't guaranteed to be -1, use the EOF macro instead.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    114
    balls, didnt notice that lol, cant believe i closed main AFTER my function and didnt even notice!

  6. #6
    Registered User
    Join Date
    May 2004
    Posts
    114
    I told it to print out the choice, and it does say a if you put a in, but it still says invalid choice. Not sure what ive done wrong

    Code:
    #include <stdio.h>
    
    void addemail();
    
    main()
    {
        int choice;
        
        printf("Email whitelist");
        printf("(a) to add email, (c) to check email against list, (e) to exit.\n");
        printf("Choice: ");
        scanf("%c",&choice);
        
        printf("%c",choice);
        
        switch (choice)
        {
            case 'a':
                addemail();
                break;
            case 'c':
               /* checkemail(); */
                break;
            case 'e':
                break;
            default:
                printf("Invalid Choice.");
                break;
        } 
    }       
        
    void addemail()
    {
        char buf[40];
        FILE *fp;
            
        printf("Enter email: ");
        fgets(buf, sizeof(buf), stdin);
            
        fp = fopen("emails","a+");
        fputs(buf,fp);
        fclose(fp);
            
        printf("\nEmail address %s added.",buf);
    }

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > scanf("%c",&choice);
    %c means char, and choice is an int

    You're also using fgets elsewhere, so its going to mess up eventually. Just stick to fgets for everything

    Code:
    int main()  /* be specific about the return type */
    {
        char choice[20];
        
        printf("Email whitelist");
        printf("(a) to add email, (c) to check email against list, (e) to exit.\n");
        printf("Choice: ");
        fgets( choice, sizeof choice, stdin );
        switch (choice[0])
    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.

  8. #8
    Registered User
    Join Date
    May 2004
    Posts
    114
    the fgets idea works nicely thanks. I thought it was better to use int for single characters, i cant remember where i read it though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM