Help! I am totally stuck. I need to open a file, then bubble sort it according to manufacturer. Plus I think my program is all screwed up form the functions on down. I must use this setup or I fail the program. Any help with the functions and the sort and search would be great. I have to sort by manufacturer and M.S.R.P. and also do a linear search for them. Thanks!
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct
{
char name[80];
char type[80];
double msrp;
double invoice;
} record;
int menu ();
void decideFile (int);
void createFile (FILE *);
void loadFile(FILE * ,struct record );
void printfFile(FILE *);
void sortFile(FILE *, struct record);
void sortManu(record);
void sortMSRP(record);
void searchFile(FILE *,struct record);
void searchManu(record);
void searchMSRP( record);
FILE *filePtr;
int main()
{
int flg=0;
int choice;
while(flg==0)
choice=menu();
if(choice==6)
flg=1;
else
menu(choice);
return 0;
}
int menu ()
{
int menuchoice;
printf("Enter 1 to create data file: \n");
printf("Enter 2 to load data to arrays: \n");
printf("Enter 3 to print report: \n");
printf("Enter 4 to sort file: \n");
printf("Enter 5 to search file: \n");
printf("Enter 6 to exit: \n");
printf("Enter choice: \n");
scanf("%d", &menuchoice);
return menuchoice;
}
void decideFile( int num)
{
int menuchoice;
switch (menuchoice)
{
case 1:
createFile(filePtr);
break;
case 2:
loadFile(filePtr,struct record);
break;
case 3:
printFile(filePtr);
break;
case 4:
sortFile(filePtr,struct record);
break;
case 5:
searchFile(filePtr,struct record);
break;
case 6:
exit(1);
break;
default:
printf("Incorrect number! Please enter again!\n");
break;
}
}
void createFile(FILE *filePtr)
{
int flag=TRUE;
record car;
filePtr=fopen("a:\\myfile.dat","w");
while(flag)
{
printf("\nEnter manufacturer (Enter \'END\' when finished): ");
scanf("%[^\n]",car.name);
if (strcmp(car.name,"END")==0);
break;
printf("\nEnter type: ");
scanf("%s",&car.type);
printf("\nEnter invoice:$ ");
scanf("%lf",&car.invoice);
printf("\nEnter M.S.R.P.: $ ");
scanf("%lf",&car.msrp);
fwrite(&car,sizeof(record),1,filePtr);
strset(car.name,' ');
}
fclose(filePtr);
return 0;
}
void loadFile(FILE *filePtr,struct record auto [])
{
record car;
int index,i;
if ((filePtr=fopen("a:\\myfile.dat","r"))==NULL);
printf("\nERROR-can't open file!");
else
{
index=0;
fread
return 0;
}
void printFile(FILE *filePtr)
{
int num;
printf("Enter 1 to print file or 2 to exit:\n ");
scanf("%d",&num);
if(num==1)
}
void sortFile(FILE *filePtr,struct record auto[])
{
int num;
printf("Enter 1 to sort by manufacturer, 2 to sort by M.S.R.P., or 3 to exit: \n");
scanf("%d",&num);
if (num==1)
sortManu(FILE *filePtr,record );
else if (num==2)
sortMSRP(FILE *filePtr, record );
else
return 0;
}
void sortManu(FILE *filePtr,struct record auto[])
}
if ((filePtr=fopen("a:\\myfile.dat","r"))==NULL);
printf("\nERROR-can't open file!");
else
{
fscanf(filePtr,"%s",&auto.name);
record car;
int pass,j,hold;
for(pass=1,pass
}
void searchFile(FILE *filePtr)
{
int num;
printf("Enter 1 to search by manufacturer, 2 to search by M.S.R.P.: \n");
scanf("%d", &num);
}