int main(){
int opt,n;
struct orders ord;
struct orders order;
char saveBill = 'y',contFlag = 'y';
char name[50];
FILE *fp;
while(contFlag == 'y'){
system("clear");
float total = 0;
int invoiceFound = 0;
printf("\t============ADV. RESTAURANT============");
printf("\n\nPlease select your prefered operation");
printf("\n\n1.Generate Invoice");
printf("\n2.Show all Invoices");
printf("\n3.Search Invoice");
printf("\n4.Exit");
printf("\n\nYour choice:\t");
scanf("%d",&opt);
fgetc(stdin);
switch(opt){
case 1:
system("clear");
printf("\nPlease enter the name of the customer:\t");
fgets(ord.customer,50,stdin);
ord.customer[strlen(ord.customer)-1] = 0;
strcpy(ord.date,__DATE__);
printf("\nPlease enter the number of items:\t");
scanf("%d",&n);
ord.numOfItems = n;
for(int i=0;i<n;i++){
fgetc(stdin);
printf("\n\n");
printf("Please enter the item %d:\t",i+1);
fgets(ord.itm[i].item,20,stdin);
ord.itm[i].item[strlen(ord.itm[i].item)-1]=0;
printf("Please enter the quantity:\t");
scanf("%d",&ord.itm[i].qty);
printf("Please enter the unit price:\t");
scanf("%f",&ord.itm[i].price);
total += ord.itm[i].qty * ord.itm[i].price;
}
generateBillHeader(ord.customer,ord.date);
for(int i=0;i<ord.numOfItems;i++){
generateBillBody(ord.itm[i].item,ord.itm[i].qty,ord.itm[i].price);
}
generateBillFooter(total);
printf("\nDo you want to save the invoice [y/n]:\t");
scanf("%s",&saveBill);
if(saveBill == 'y'){
fp = fopen("RestaurantBill.dat","a+");
fwrite(&ord,sizeof(struct orders),1,fp);
if(fwrite != 0)
printf("\nSuccessfully saved");
else
printf("\nError saving");
fclose(fp);
}
break;
case 2:
system("clear");
fp = fopen("RestaurantBill.dat","r");
printf("\n *****Your Previous Invoices*****\n");
while(fread(&order,sizeof(struct orders),1,fp)){
float tot = 0;
generateBillHeader(order.customer,order.date);
for(int i=0;i<order.numOfItems;i++){
generateBillBody(order.itm[i].item,order.itm[i].qty,order.itm[i].price);
tot+=order.itm[i].qty * order.itm[i].price;
}
generateBillFooter(tot);
}
fclose(fp);
break;
What I have tried:
when i re-enter that variable in case 2 it works but it doesn't work when i export file with different data