Structures and File Organisation
Hi, my friends and I are currently doing an assignment on Structures and File Organisations. The program is suppose to display the details of the supplier who has the lowest quotation.
A friend of ours helped us out with the coding but I can't figure out what went wrong because it wouldn't display the output properly.
A friend of ours helped us out with the coding but I can't figure out what went wrong because it wouldn't display the output properly.
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
FILE *fp;
char strName[20], strAddress[40], strSupplierNo[10],strPrice[8];
struct supplier
{
int tenderID;
int supplierNo;
char name[20];
char address[40];
float quotationPrice;
};
typedef struct supplier Sup;
Sup dealer[8];
void Initialize_Structure();
void Data_Entry();
void Write_To_File();
void Read_From_File();
void Display_Lowest_Supplier(int k);
int Find_Lowest_Quotation();
main()
{
int x;
Initialize_Structure();
Data_Entry();
Write_To_File();
Read_From_File();
x = Find_Lowest_Quotation();
Display_Lowest_Supplier(x);
return 0;
}
void Initialize_Structure()
{
int i;
for (i=0;i<8;i++)
{
dealer[i].tenderID = 0;
dealer[i].supplierNo = 0;
strcpy(dealer[i].name,"");
strcpy(dealer[i].address,"");
dealer[i].quotationPrice = 0.0;
}
}
void Data_Entry()
{
int j;
printf("*** Tendering System to Supply Computers to school *** \n\n");
for (j=0;j<8;j++)
{
printf("Tender Identifcation Number : ");
printf("%d",j+1);
printf("\n");
printf("Supplier Number : ");
gets(strSupplierNo);
printf("Name : ");
gets(strName);
printf("Address : ");
gets(strAddress);
printf("Quotation Price : ");
gets(strPrice);
printf("\n");
dealer[j].tenderID = j+1;
dealer[j].supplierNo = atoi(strSupplierNo);
strcpy(dealer[j].name,strName);
strcpy(dealer[j].address,strAddress);
dealer[j].quotationPrice = atof(strPrice);
}
}
void Write_To_File()
{
int k;
if ((fp=fopen("DEALER.TXT","w+t"))==NULL)
{
printf("\aError opening DEALER.TXT\n");
abort();
}
for (k=0;k<8;k++)
{
fprintf(fp,"%d\n",dealer[k].tenderID);
fprintf(fp,"%d\n",dealer[k].supplierNo);
fprintf(fp,"%s\n",dealer[k].name);
fprintf(fp,"%s\n",dealer[k].address);
fprintf(fp,"%f\n",dealer[k].quotationPrice);
}
fclose(fp);
}
void Read_From_File()
{
Initialize_Structure();
int k;
if ((fp=fopen("DEALER.TXT","r+t"))==NULL)
{
printf("\aError opening DEALER.TXT\n");
abort();
}
for (k=0;k<8;k++)
{
fscanf(fp,"%d\n",&dealer[k].tenderID);
fscanf(fp,"%d\n",&dealer[k].supplierNo);
fgets(dealer[k].name,20,fp);
fgets(dealer[k].address,40,fp);
fscanf(fp,"%f",&dealer[k].quotationPrice);
}
/* close file */
fclose(fp);
}
void Display_Lowest_Supplier(int k)
{
printf("The lowest quotation price from Supplier : \n\n");
printf("Tender ID Number : %d\n",dealer[k].tenderID);
printf("Supplier Number : %d\n",dealer[k].supplierNo);
printf("Supplier Name : %s",dealer[k].name);
printf("Supplier Address : %s",dealer[k].address);
printf("Quotation Price : %.2f\n\n",dealer[k].quotationPrice);
printf(" **** End **** \n");
}
int Find_Lowest_Quotation()
{
float fltAmount = dealer[0].quotationPrice;
int s = 0;
int m = 0;
for (m=1;m<8;m++)
{
if (fltAmount < dealer[m].quotationPrice)
{
fltAmount;
s;
}
else
{
fltAmount = dealer[m].quotationPrice;
s = m;
}
}
return s;
}
