Thread: Help on program

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    1

    Question Help on program

    Hi ALL. Maybe someone can give me a hand. I was trying to write a CD collection program, and I got stuck while compiling the program. I came up with an error that said:

    " c:\documents and settings\ishbu\desktop\c\cd3.c: line 84: illegal dereference 'add_it(cditem)'
    aborting compile "


    the whole code is here...so maybe someone can take a look in their spare time.

    Also one other thing of note...in the switch process, the program never breaks when it is supposed to. I found that out by just testing bits of the code at a time.

    Here it is:

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    struct cd_struct
      { char title[30];
        char artist[30];
      };
    
    FILE *fp;
    
    #define MAX 50
    #define BELL '\x07'
    #define FILENAME "CDs.dat"
    
    
    void main(void);
    void disp_menu(void);
    void clear_sc(void);
    void enter_cd(void);
    void print_cd(void);
    void err_msg(char err_msg[]);
    void add_it(struct cd_struct cditem);
    void pause_sc(void);
    void pr_data(struct cd_struct cditem);
    
    
    /******************************/
    void main(void)
    {
      struct cd_struct list[MAX];
      int num;
      
     do
        { disp_menu();
          scanf(" %d", &num);
          
          switch (num)
          { case 1 : { enter_cd();
                         break; }
            case 2 : { print_cd();
                         break; }
            case 3 : { break; }
            default : { err_msg("*** You need to enter");
                        printf(" 1 through 3 ***");
                        break; }
          }
         } while (num!=3);
         return;
    }
    
    /******************************/
    void disp_menu()
    {
      clear_sc();
      printf("\t\t*** CD LIST MANAGER ***\n");
      printf("\t\t    ---------------\n\n\n\n");
      printf("Do you want to:\n\n\n");
      printf("\t1. Add a CD to your collection\n\n\n");
      printf("\t2. Print info of CDs in your collection\n\n\n");
      printf("\t3. Exit this program\n\n\n");
      printf("What is your choice? ");
      return;
    }
    
    /******************************/
    void clear_sc()
    {
     int ctr;
     for(ctr=0;ctr<25;ctr++)
        { printf("\n"); }
     return;
    }
    
    /******************************/
    void enter_cd(void)
    {
      struct cd_struct cditem;
      char answ;
      do
      { fflush(stdin);
        printf("\n\n\n\n\nWhat is the TITLE? ");
        gets(cditem.title);
        printf("Who is the Artist? ");
        gets(cditem.artist);
        add_it(cditem);
        printf("\n\nDo you want to enter another CD? (Y/N) ");
        answ=getchar();
        getchar();
       }while (toupper(answ)=='Y');
    
       return;
    }
    
    /******************************/
    void print_cd(void)
    {
      struct cd_struct cditem;
      int s, linectr=0;
    
      s = sizeof(struct cd_struct);
      if((fp=fopen(FILENAME, "r"))==NULL)
         { err_msg("*** Read error--ensure file exists ***");
           return 0; }
      do
        { if(fread(&cditem, sizeof(struct cd_struct),1, fp)!=s)
         { if(feof(fp))
            { break; }
         }
           if (linectr>20)
           { pause_sc;
             linectr=0; }
           pr_data(cditem);
           linectr+=4;
        }while (!feof(fp));     
      fclose(fp);
      printf("\n- End of Collection -");
      pause_sc();
      return;
    }
    
    /******************************/
    void err_msg(char err_msg[])
    {
      printf("\n\n%s\n", err_msg);
      printf("%c", BELL);
      return;
    }
    
    /******************************/
    void add_it(struct cd_struct cditem)
    {
      if((fp = fopen(FILENAME, "a"))==NULL)
         { err_msg("*** Disk error--please check disk drive ***");
           return 0; }
      fwrite(&cditem, sizeof(cditem), 1, fp);
      fclose(fp);
      return;
    }
    
    /******************************/
    void pause_sc()
    {
      char ans;
      fflush(stdin);
      printf("\nPress the Enter key to continue...");
      ans=getchar();
      fflush(stdin);
      return;
    }
    
    /******************************/
    void pr_data(struct cd_struct cditem)
    {
      printf("\nTitle    :  %-30s\n", cditem.title);
      printf("Artist    :  %-30s\n", cditem.artist);
      return;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Read this.
    Then read this.
    While you're at it, read this.

    That'll get you started.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    try experimenting with the brackets in your switch statement

  4. #4
    Quote Originally Posted by rewclaus
    Here it is: <...>
    First of all, you sould learn how to configure your compiler for a better analysis :
    Code:
    Compiling MAIN.C:
    Error MAIN.C 28: main must have a return type of int
    Warning MAIN.C 63: 'list' is declared but never used
    Warning MAIN.C 105: Code has no effect
    Warning MAIN.C 122: Void functions may not return a value
    Warning MAIN.C 135: Code has no effect
    Warning MAIN.C 145: Both return and return with a value used
    Warning MAIN.C 162: Void functions may not return a value
    Warning MAIN.C 166: Both return and return with a value used
    Warning MAIN.C 178: 'ans' is assigned a value that is never used
    That said, there are several flaws in your code, mainly a bad use of the input functions. I recommend to use solid input functions based on fgets() or fgetc(), and to stick them into some personal library.

    Here is a revisited version of your code using such a library code (ful references given further).
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <stdlib.h>
    
    #include "ed/inc/io.h"
    
    struct cd_struct
    {
       char title[30];
       char artist[30];
    };
    
    #define MAX 50
    #define BELL '\a'
    #define FILENAME "CDs.dat"
    
    /******************************/
    static void clear_sc (void)
    {
    #if 1
       /* MS-DOS/Windows */
       system ("cls");
    #else
       /* Unix-like */
       system ("clear");
    #endif
    }
    
    /******************************/
    static void disp_menu (void)
    {
       clear_sc ();
       printf ("\t\t*** CD LIST MANAGER ***\n"
               "\t\t    ---------------\n\n\n\n"
               "Do you want to:\n\n\n"
               "\t1. Add a CD to your collection\n\n\n"
               "\t2. Print info of CDs in your collection\n\n\n"
               "\t3. Exit this program\n\n\n"
               "What is your choice? ");
    }
    
    /******************************/
    static void err_msg (char err_msg[])
    {
       printf ("\n\n%s\n", err_msg);
       printf ("%c", BELL);
    }
    
    /******************************/
    static int add_it (struct cd_struct const *p_cditem)
    {
       int ok = 1;
       FILE *fp = fopen (FILENAME, "ab");
    
       if (fp == NULL)
       {
          err_msg ("*** Disk error--please check disk drive ***");
          ok = 0;
       }
       else
       {
          fwrite (p_cditem, sizeof *p_cditem, 1, fp);
          fclose (fp);
       }
       return ok;
    }
    
    /******************************/
    static void enter_cd (void)
    {
       struct cd_struct cditem;
       int answ;
       do
       {
          printf ("\n\n\n\n\nWhat is the TITLE? ");
          get_s (cditem.title, sizeof cditem.title);
          printf ("Who is the Artist? ");
          get_s (cditem.artist, sizeof cditem.artist);
          add_it (&cditem);
          printf ("\n\nDo you want to enter another CD? (Y/N) ");
          answ = get_c ();
       }
       while (toupper (answ) == 'Y');
    }
    
    /******************************/
    static void pause_sc (void)
    {
       printf ("\nPress the Enter key to continue...");
       (void) get_c ();
    }
    
    /******************************/
    static void pr_data (struct cd_struct const *p_cditem)
    {
       printf ("\nTitle    :  %-30s\n", p_cditem->title);
       printf ("Artist    :  %-30s\n", p_cditem->artist);
    }
    
    /******************************/
    static int print_cd (void)
    {
       int ok = 1;
       struct cd_struct cditem;
       int linectr = 0;
       size_t s = sizeof (struct cd_struct);
       FILE *fp = fopen (FILENAME, "rb");
    
       if (fp == NULL)
       {
          err_msg ("*** Read error--ensure file exists ***");
          ok = 0;
       }
       else
       {
          do
          {
             if (fread (&cditem, sizeof (struct cd_struct), 1, fp) != s)
             {
                if (feof (fp))
                {
                   break;
                }
             }
    
             if (linectr > 20)
             {
                pause_sc ();
                linectr = 0;
             }
             pr_data (&cditem);
             linectr += 4;
          }
          while (!feof (fp));
          fclose (fp);
          printf ("\n- End of Collection -");
          pause_sc ();
       }
       return ok;
    }
    
    /******************************/
    int main (void)
    {
       int num;
    
       do
       {
          disp_menu ();
          {
             long x;
             get_l (&x);
             num = (int) x;
          }
    
          switch (num)
          {
          case 1:
             {
                enter_cd ();
                break;
             }
          case 2:
             {
                print_cd ();
                break;
             }
          case 3:
             {
                break;
             }
          default:
             {
                err_msg ("*** You need to enter");
                printf (" 1 through 3 ***");
                break;
             }
          }
       }
       while (num != 3);
       return;
    }
    The missing code is here:

    http://mapage.noos.fr/emdel/clib.htm
    Module IO

    Feel free to ask for details.

    Note also that storing raw structures is not portable in C. It only works by chance on a very closed system (same machine, same compiler, same options, same phases of moon...)

    The usual way is to use a text format.
    Emmanuel Delahaye

    "C is a sharp tool"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM