Thread: Help! ERROR ALERT!

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    6

    Help! ERROR ALERT!

    Hi
    need help as below is my code for linking lists. It keeps coming up with errors. I dont understand them how to get rid of them. Been trying for hours. Can someone please help me see where I am going wrong cos really having problems.

    x Jit x




    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    struct node
            {
            char name[20];
            struct node*link;
            };
            
    void add();
    void delete();
    void display();
    struct node*load();
    void save();
    
    void main(void)
     {
            struct node*start;  
            char inbuf[2];
            char choice;
            int check;
            /*set up dummy first node*/
            start=(struct node*)malloc(sizeof(struct node));
            strcpy(start->name,"");
            start->link=NULL;
            do
             {
                    printf("Linked List with Pointers\n");
                    printf("Add new item...........1\n");
                    printf("Delete item....2\n");
                    printf("Display list........3\n");
                    printf("Load from disk..........4\n");
                    printf("Save to disk...........5\n");
                    printf("Exit from program..........6\n");  
                    gets(inbuf);
                    choice=inbuf[0];
                    
                    
                  
                    switch(choice)
                    {
                            case ('1'):add(start);          /*pass ptr to start of list*/
                                    break;
                            case ('2'):delete(start);break;
                            case ('3'):display(start);break;
                            case ('4'):start=load();break;                  
                            case ('5'):save(start);break;                                  
                            case ('6'):break;
                            default   :printf("Unknown Option %c\n",choice);             
                                 
                          
                     }
                     
             }
             while(choice!='6');
             
    }
    
    void add(struct node*start)
    {
            char newname[20];
            struct node*next;
            struct node*temp;
            struct node*prev;
            
            temp=(struct node*)malloc(sizeof(struct node));
            /*allocate storage*/
            if(temp==NULL)
            {
                    printf("Out of memory space\n");
                    return;
            } 
            printf("Item to add:");
            gets(newname);
            strcpy(temp->name,newname);
            next=start;
            while(next->link!=NULL&&strcmp(next->name,newname)<0)
            {
                    prev=next;              /*store current position*/
                    next=next->link;        /*and find next*/
            }
            if(strcmp(next->name,newname)<0)
                    prev=next;              /*if before end*/
            temp->link=prev->link;          /*adjust pointers*/
            prev->link=temp;
            
    }
    
    void delete(struct node*next)
    {
            char newname[20];
            struct node*prev=next;
            
            printf("Item to delete:");
            gets(newname);
            while(strcmp(next->name,newname)<0&&next->link!=NULL)
            {
                    prev=next;
                    next=next->link;
            }
            if(strcmp(next->name,newname)1=0)
                    printf("%s not found./n",newname);
            else
            {
                    printf("Deleting %s\n",next->name);
                    prev->link=next->link;
                    free(next);             /*return storage to free memory*/
            }
    }
    
    void display(struct node*next)
    {
            do
            {
                    printf("%s\n",next->name);
                    next=next->link;
            }
            while(next!=NULL);
    }
    
    struct node*load()                      /*this returns ptr to first item*/
    {
            FILE*fin;
            char name[20];
            struct node*start;
            struct node*next;
            struct node*prev;
            
            fin=fopen("listdata","r");
            start=(struct node*)malloc(sizeof(struct node));
            strcpy(start->name,"");         /*dummy to start list*/
            prev=start;
            fscanf(fin,"%s\n",name);
            
            do
            {
                    next=(struct node*)malloc(sizeof(struct node));
                    strcpy(next->name,name);
                    prev->link-next;
                    prev=next;
            }
            while(fscan(fin,"%s\n",name)!=EOF);
                    fclose(fin);
            next->link=NULL;                /*mark end of list*/
            return(start);
    }
    
    void save(struct node*next)
    {
            FILE*fout;
            char*temp;
            
            next=next->link;                /*ignore first non-item*/
            fout=fopen("listdata","w");
            while(next!=NULL)
            {
                    fprintf(fout,"%s\n",next->name);
                    next=next->link;
            }
            fclose(fout);
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    1. Code:
      if(strcmp(next->name,newname)1=0)
      Code:
      if(strcmp(next->name,newname)!=0)
    2. Code:
      while(fscan(fin,"%s\n",name)!=EOF);
      Code:
      while(fscanf(fin,"%s\n",name)!=EOF);
    [edit]FAQ > Explanations of... > Why it's bad to use feof() to control a loop

    These are not prototypes in C.
    Code:
    void add();
    void delete();
    void display();
    struct node*load();
    void save();
    Make them match their definitions, use void if there are no parameters.

    void main()

    [more edits]
    Code:
    start=(struct node*)malloc(sizeof(struct node));
    Check the return value and...

    FAQ > Explanations of... > Casting malloc
    Last edited by Dave_Sinkula; 05-05-2005 at 11:21 AM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,541
    void main, using gets() and casting malloc.

    Looks like a heavy dose of FAQ is required.

    1. Posts titles like "Help!" are useless, did you read the "read before posting" bits?

    2. Saying there are errors without actually saying what those errors are is similarly useless as well.

    3. You didn't mention your OS or compiler.

    On the plus side, you did manage the code tags.
    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
    Registered User
    Join Date
    Mar 2005
    Posts
    6

    Salford Plato 2 compiler

    HI

    by the way my operating system is windows and the compiler is Salford Plato 2. If that helps

    x Jit x

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Stop SPAMMING the forums.

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

Popular pages Recent additions subscribe to a feed