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*/
}
}
}