【解決方法】ケース 1 では numofitems を入力しましたが、ケース 2 では消えましたか? 説明と修正方法を教えてください。

プログラミングQA


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;

私が試したこと:

when i re-enter that variable in case 2 it works but it doesn't work when i export file with different data

解決策 1

ケース 2 使用しない n (最初にユーザーから値をロードした場所) または ord 値を保存した場所 n.

ordorder 個別の変数です。 彼らは価値観を共有していません!

また、自分自身に好意を持ってください: コードをインデントしてください! 現状では、何がどのブロックの一部で何がそうでないかについて混乱するのは簡単です…
そして、本当により良いコーディングをしたい場合は、コードをリファクタリングして、コードを作成する代わりに、ケースごとに呼び出す関数を使用します。 main モノリシック関数 – このような問題をより目立たせるだけでなく、コードを読みやすく、作業しやすくします。

解決策 2

私はあなたのコードを少し当て推量で使用しました*。正しく動作します。 したがって、どのような結果が表示されたとしても、表示されていないコードの部分と何らかの関係があるはずです。

*注文用に次の構造を作成しました。

C++
struct ii
{
    char item[20];
    int qty;
    float price;
};

struct orders
{
    char customer[50];
    char date[16];
    int numOfItems;
    struct ii itm[3];
};

コメント

タイトルとURLをコピーしました