Click here to Skip to main content
15,886,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
int main(){
    
    int opt,n;
    struct orders ord;
    struct orders order;
    char saveBill = 'y',contFlag = 'y';
    char name[50];
    FILE *fp;
       //dashboard
    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
Posted
Updated 23-Dec-22 6:01am
Comments
Richard MacCutchan 23-Dec-22 11:29am    
What is the definition of the orders structure?
i want to code better 25-Dec-22 3:40am    
struct items{
char item[30];
int qty;
float price;
};
struct orders{
char customer[50];
char date[50];
int numOfItems;
struct items itm[50];

I just used your code with a bit of guesswork* and it works correctly. So whatever results you see must have something to do with the parts of the code that you have not shown us.

*I created the following structures for the orders:
C++
struct ii
{
    char item[20];
    int qty;
    float price;
};

struct orders
{
    char customer[50];
    char date[16];
    int numOfItems;
    struct ii itm[3];
};
 
Share this answer
 
Case 2 doesn't use n (where you initially loaded the value from the user) or ord where you saved the value in n.

ord and order are separate variables; they do not share values!

Also, do yourself a favour: indent your code! as it stands, it's easy to get confused about what is and isn't a part of which block...
And if you really want to code better, then refactor your code to use functions that you call for each case instead of making main a monolithic function - it'll not only make problems like this a lot more visible, it'll make your code easier to read and work on as well.
 
Share this answer
 
Comments
i want to code better 25-Dec-22 3:39am    
First I would like to thank you for your advice to me. But the thing that I find it is not correct is that I tried your way and I see that in the case of 2 the value of n does not appear. So can you suggest me a workaround?
i want to code better 25-Dec-22 3:42am    
and I try to enter the value of n in case 2, it can run but I find it inefficient because the file has different data of n, it prints the gain and loss.
OriginalGriff 25-Dec-22 6:32am    
Have a look at your code - a good close look - and then read what I said again ...
i want to code better 25-Dec-22 9:02am    
I deleted the "order" and changed everything in case 2 to "ord" as you said, but I see no change :(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900