Thread: Please Help!!!

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

    Please Help!!!

    Hi
    its me again having prob with getting program to work can anyone help. Keep getting errors, tried everything I know. Really cant get program to run properly.

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    int load();
    void add();
    void displayall();
    void fineone();
    void update();
    ;
    void sort();
    
     FILE*fin,*fout;
     struct card
                {
                char name[30];
                float salary;
                int holidays;
                }employee[10];     /*or as many as you want*/
                
     struct card temp;
     int count=0;
     int size;
     int choice=0;
     char dummy[30];
     
     void main(void)
     {
            int loop;
            
             size=sizeof(struct card);      /*gives size in bytes*/
             
             do
             {
                    printf("\n\nFile Manager\n\n");
                    printf("Load File from Disk....1\n");
                    printf("Add a Record...........2\n");
                    printf("Display All Records....3\n");
                    printf("Find One Record........4\n");
                    printf("Update Record..........5\n");
                    printf("Delete Record..........6\n");
                    printf("Sort by Name...........7\n");
                    printf("Save and Exit..........8\n");
                    
                    do
                    {
                            printf("\nEnter Choice:");
                            fgets(dummy,30,stdin);
                            choice=atoi(dummy);
                    }
                    while(choice<1&&choice>8); 
                    
                    switch(choice)
                    {
                            case 1: count=load();   break;
                            case 2: add();          break;
                            case 3: displayall();   break;
                            case 4: findone(1);             /*these all start with find*/
                                    break;                  /*1 for display option*/
                            case 5: findone(2);             /*2 for update option*/
                                    break;                                  
                            case 6: findone(3);             /*3 for delete option*/
                                    break;
                            case 7: sort();         break;
                            case 8: ;
                     }
                     
             }
                    while(choice!=8);
                    fout=fopen("test.dat","wb");     /*note write mode*/
                    fwrite(employee,size,count,fout);
                    fclose(fout);
    }
    
    void add()
    {
            do
            {
                    printf("Name:");
                    fgets(temp.name,30,stdin);      /*scanf no good for multiple names*/
                    printf("Salary:");
                    fgets(dummy,30,stdin);          /*fgets throughout for consistency*/
                    temp.salary=atof(dummy);
                    printf("Holiday Entitlement:");
                    fgets(dummy,30,stdin);
                    temp.holidays=atoi(dummy);
                    employee[count]=temp;
                    count++;
                    printf("More(y/n)");
                    fgets(dummy,30,stdin);
             }
             while(strcmp(dummy,"n\n")!=0);         /*fgets keeps the \n*/
    }
    
    int load()
    {
            int loop=0;
            fin=fopen("test.dat","rb");              /*read mode*/
            while(fread(&employee[loop],size,1,fin)!=NULL)
                    loop++;                         /* take count*/
            fclose(fin);
            return loop;                            /*return count to main()*/
    }
    
    void displayall()
    {
            int loop;
            
            for(loop=0;loop<count;loop++)
                    display(loop);
    }
    
    void findone(int option)
    {
            int loop=0;
            int found=0;
            
            printf("\nEnter employee name:");
            fgets(dummy,30,stdin);
            for(loop=0;loop<count&&found==0;loop++)
                    if(strcmp(dummy,employee[loop].name)==0)
                            found=1;
            if(found==0)
                    printf("No one here called %s\n",dummy);
            else
            {
                    loop--;                 /*undo last loop++*/
                    if(option==1)
                            display(loop);
                    else if(option==2)
                            update(loop);
                         else
                            delete(loop);
           }     
    }
    
    void display(int which)
    {
            printf("%s\t",employee[which].name);
            printf("%.2f\t",employee[which].salary);
            printf("%d\n",employee[which].holidays);
    }
    
    void update(int which)
    {   
            int loop;
            
            printf("Name:%s New Name or [Return]:",employee[which].name);
            fgets(temp.name,30,stdin);
            if(strlen(temp.name)>1)
                    strcpy(employee[which].name,temp.name);
            printf("Salary%.2f New Value or 0:",employee[which].salary);
            fgets(dummy,30,stdin);
            temp.salary=atof(dummy);
            if(temp.salary>0)
                    employee[which].salary=temp.salary;
            printf("Holidays%d New Value or 0:",employee[which].holidays);
            fgets(dummy,30,stdin);
            temp.holidays=atoi(dummy);
            if(temp.holidays>0)
                    employee[which].holidays=temp.holidays;
    }
    
    void delete(int which)
    {
            int loop;
            
            count--;
            for(loop=which;loop<=count;loop++)              /*overwrite with*/
                    employee[loop]=employee[loop+1];        /*the one below*/
    }
    
    void sort(void)
    {
            int loop;
            int done=1;                                     /*finished sorting? flag*/
            
            while(done==1)
            {
                    done=0;                                  /*assumed sorted*/
                    for(loop=0;loop<count-1;loop++)
                            if(strcmp(employee[loop].name,employee[loop+1].name)>0)
                            {
                                    temp=employee[loop];
                                    employee[loop]=employee[loop+1];
                                    employee[loop+1]=temp;
                                    done=1;                 /*no it is't*/
                                    
                            }   
            }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Keep getting errors
    Such as what (didn't I just do this sketch?)
    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.

  3. #3
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    look

    Code:
    /* testjit01.c */
    
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    int load();
    void add();
    void displayall();
     fineone();
     update();
    ;
    void sort();
    
     FILE*fin,*fout;
     struct card
                {
                char name[30];
                float salary;
                int holidays;
                }employee[10];     /*or as many as you want*/
                
     struct card temp;
     int count=0;
     int size;
     int choice=0;
     char dummy[30];
     
     void main(void)
     {
            int loop;
            
             size=sizeof(struct card);      /*gives size in bytes*/
             
             do
             {
                    printf("\n\nFile Manager\n\n");
                    printf("Load File from Disk....1\n");
                    printf("Add a Record...........2\n");
                    printf("Display All Records....3\n");
                    printf("Find One Record........4\n");
                    printf("Update Record..........5\n");
                    printf("Delete Record..........6\n");
                    printf("Sort by Name...........7\n");
                    printf("Save and Exit..........8\n");
                    
                    do
                    {
                            printf("\nEnter Choice:");
                            fgets(dummy,30,stdin);
                            choice=atoi(dummy);
                    }
                    while(choice<1&&choice>8); 
                    
                    switch(choice)
                    {
                            case 1: count=load();   break;
                            case 2: add();          break;
                            case 3: displayall();   break;
                            case 4: findone(1);             /*these all start with find*/
                                    break;                  /*1 for display option*/
                            case 5: findone(2);             /*2 for update option*/
                                    break;                                  
                            case 6: findone(3);             /*3 for delete option*/
                                    break;
                            case 7: sort();         break;
                            case 8: ;
                     }
                     
             }
                    while(choice!=8);
                    fout=fopen("test.dat","wb");     /*note write mode*/
                    fwrite(employee,size,count,fout);
                    fclose(fout);
    }
    
    void add()
    {
            do
            {
                    printf("Name:");
                    fgets(temp.name,30,stdin);      /*scanf no good for multiple names*/
                    printf("Salary:");
                    fgets(dummy,30,stdin);          /*fgets throughout for consistency*/
                    temp.salary=atof(dummy);
                    printf("Holiday Entitlement:");
                    fgets(dummy,30,stdin);
                    temp.holidays=atoi(dummy);
                    employee[count]=temp;
                    count++;
                    printf("More(y/n)");
                    fgets(dummy,30,stdin);
             }
             while(strcmp(dummy,"n\n")!=0);         /*fgets keeps the \n*/
    }
    
    int load()
    {
            int loop=0;
            fin=fopen("test.dat","rb");              /*read mode*/
            while(fread(&employee[loop],size,1,fin)!=NULL)
                    loop++;                         /* take count*/
            fclose(fin);
            return loop;                            /*return count to main()*/
    }
    
    void displayall()
    {
            int loop;
            
            for(loop=0;loop<count;loop++)
                    display(loop);
    }
    
    findone(int option)
    {
            int loop=0;
            int found=0;
            
            printf("\nEnter employee name:");
            fgets(dummy,30,stdin);
            for(loop=0;loop<count&&found==0;loop++)
                    if(strcmp(dummy,employee[loop].name)==0)
                            found=1;
            if(found==0)
                    printf("No one here called %s\n",dummy);
            else
            {
                    loop--;                 /*undo last loop++*/
                    if(option==1)
                            display(loop);
                    else if(option==2)
                            update(loop);
                         else
                            delete(loop);
           }     
    }
    
     display(int which)
    {
            printf("%s\t",employee[which].name);
            printf("%.2f\t",employee[which].salary);
            printf("%d\n",employee[which].holidays);
    }
    
     update(int which)
    {   
            int loop;
            
            printf("Name:%s New Name or [Return]:",employee[which].name);
            fgets(temp.name,30,stdin);
            if(strlen(temp.name)>1)
                    strcpy(employee[which].name,temp.name);
            printf("Salary%.2f New Value or 0:",employee[which].salary);
            fgets(dummy,30,stdin);
            temp.salary=atof(dummy);
            if(temp.salary>0)
                    employee[which].salary=temp.salary;
            printf("Holidays%d New Value or 0:",employee[which].holidays);
            fgets(dummy,30,stdin);
            temp.holidays=atoi(dummy);
            if(temp.holidays>0)
                    employee[which].holidays=temp.holidays;
    }
    
     delete(int which)
    {
            int loop;
            
            count--;
            for(loop=which;loop<=count;loop++)              /*overwrite with*/
                    employee[loop]=employee[loop+1];        /*the one below*/
    }
    
    void sort(void)
    {
            int loop;
            int done=1;                                     /*finished sorting? flag*/
            
            while(done==1)
            {
                    done=0;                                  /*assumed sorted*/
                    for(loop=0;loop<count-1;loop++)
                            if(strcmp(employee[loop].name,employee[loop+1].name)>0)
                            {
                                    temp=employee[loop];
                                    employee[loop]=employee[loop+1];
                                    employee[loop+1]=temp;
                                    done=1;                 /*no it is't*/
                                    
                            }   
            }
    }

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    looking at your code in found some bugs,

    1. functon prototype not declared
    2. function not called with the proper identifier
    3. function is not defined with proper arguments
    4. function main should return an int
    5. delete is a keyword shouldn't be used for function identifier

    try to check out these error with your program and compare with mine
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    int load();
    void add();
    void displayall();
    void findone(int);
    void update(int);
    void deletee(int);
    void sort();
     void display(int);
     void update(int which);
     void delete1(int which);
    
    
     FILE*fin,*fout;
     struct card
                {
                char name[30];
                float salary;
                int holidays;
                }employee[10];     /*or as many as you want*/
                
     struct card temp;
     int count=0;
     int size;
     int choice=0;
     char dummy[30];
     
     
    int main(void)
     {
            int loop;
            
             size=sizeof(struct card);      /*gives size in bytes*/
             
             do
             {
                    printf("\n\nFile Manager\n\n");
                    printf("Load File from Disk....1\n");
                    printf("Add a Record...........2\n");
                    printf("Display All Records....3\n");
                    printf("Find One Record........4\n");
                    printf("Update Record..........5\n");
                    printf("Delete Record..........6\n");
                    printf("Sort by Name...........7\n");
                    printf("Save and Exit..........8\n");
                    
                    do
                    {
                            printf("\nEnter Choice:");
                            fgets(dummy,30,stdin);
                            choice=atoi(dummy);
                    }
                    while(choice<1&&choice>8); 
                    
                    switch(choice)
                    {
                            case 1: count=load();   break;
                            case 2: add();          break;
                            case 3: displayall();   break;
                            case 4: findone(1);             /*these all start with find*/
                                    break;                  /*1 for display option*/
                            case 5: findone(2);             /*2 for update option*/
                                    break;                                  
                            case 6: findone(3);             /*3 for delete option*/
                                    break;
                            case 7: sort();         break;
                            case 8: ;
                     }
                     
             }
                    while(choice!=8);
                    fout=fopen("test.dat","wb");     /*note write mode*/
                    fwrite(employee,size,count,fout);
                    fclose(fout);
                    
             return 0;
    }
    
    void add()
    {
            do
            {
                    printf("Name:");
                    fgets(temp.name,30,stdin);      /*scanf no good for multiple names*/
                    printf("Salary:");
                    fgets(dummy,30,stdin);          /*fgets throughout for consistency*/
                    temp.salary=atof(dummy);
                    printf("Holiday Entitlement:");
                    fgets(dummy,30,stdin);
                    temp.holidays=atoi(dummy);
                    employee[count]=temp;
                    count++;
                    printf("More(y/n)");
                    fgets(dummy,30,stdin);
             }
             while(strcmp(dummy,"n\n")!=0);         /*fgets keeps the \n*/
    }
    
    int load()
    {
            int loop=0;
            fin=fopen("test.dat","rb");              /*read mode*/
            while(fread(&employee[loop],size,1,fin)!=EOF)
                    loop++;                         /* take count*/
            fclose(fin);
            return loop;                            /*return count to main()*/
    }
    
    void displayall()
    {
            int loop;
            
            for(loop=0;loop<count;loop++)
                    display(loop);
    }
    
    void findone(int option)
    {
            int loop=0;
            int found=0;
            
            printf("\nEnter employee name:");
            fgets(dummy,30,stdin);
            for(loop=0;loop<count&&found==0;loop++){
                    if(strcmp(dummy,employee[loop].name)==0)
                            found=1;
            if(found==0)
                    printf("No one here called %s\n",dummy);
            else
            {
                    loop--;                 /*undo last loop++*/
                    if(option==1)
                            display(loop);
                    else if(option==2)
                            update(loop);
                         else
                            delete1(loop);
           }     
    }}
    
    void display(int which)
    {
            printf("%s\t",employee[which].name);
            printf("%.2f\t",employee[which].salary);
            printf("%d\n",employee[which].holidays);
    }
    
    void update(int which)
    {   
            int loop;
            
            printf("Name:%s New Name or [Return]:",employee[which].name);
            fgets(temp.name,30,stdin);
            if(strlen(temp.name)>1)
                    strcpy(employee[which].name,temp.name);
            printf("Salary%.2f New Value or 0:",employee[which].salary);
            fgets(dummy,30,stdin);
            temp.salary=atof(dummy);
            if(temp.salary>0)
                    employee[which].salary=temp.salary;
            printf("Holidays%d New Value or 0:",employee[which].holidays);
            fgets(dummy,30,stdin);
            temp.holidays=atoi(dummy);
            if(temp.holidays>0)
                    employee[which].holidays=temp.holidays;
    }
    
    void delete1(int which)
    {
            int loop;
            
            count--;
            for(loop=which;loop<=count;loop++)              /*overwrite with*/
                    employee[loop]=employee[loop+1];        /*the one below*/
    }
    
    void sort(void)
    {
            int loop;
            int done=1;                                     /*finished sorting? flag*/
            
            while(done==1)
            {
                    done=0;                                  /*assumed sorted*/
                    for(loop=0;loop<count-1;loop++)
                            if(strcmp(employee[loop].name,employee[loop+1].name)>0)
                            {
                                    temp=employee[loop];
                                    employee[loop]=employee[loop+1];
                                    employee[loop+1]=temp;
                                    done=1;                 /*no it is't*/
                                    
                            }   
            }
    }
    s.s.harish

  5. #5
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    Use one thread, not 3. Closed.
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

Popular pages Recent additions subscribe to a feed