首页 > 解决方案 > C 只读取第一个结构,不能读取其他输入

问题描述

我正在创建一个基本的杂货配送系统。如果您是第一次输入,该代码可以正常工作。但是,如果您在收据部分输入另一个 ID,尽管输入了所需的信息,但它只会打印“no delivery pending”。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct grocery {
    int number;
    char prod1[20];//products
    char prod2[20];
    char prod3[20];
    char prod4[20];
    float price1; //price of product
    float price2; 
    float price3; 
    float price4; 
    char status; //status of products
    float ttl; //total
};
struct grocery g;
FILE * fptr;
char prod[][20] = {"tissue","soap","shampoo","lotion","tampons"};
float price[5] = {1.00, 2.30, 1.50, 4.30, 5.00};
int orders(int id, int g1, int g2, int g3, int g4) {
    int a;
    fptr = fopen("delivery.txt", "a");
    if (fptr == NULL) {
        fptr = fopen("delivery.txt", "w");
    }
    fflush(stdin);
    g.number = id;
    fflush(stdin);
    //copies file 
    strcpy(g.prod1, prod[g1 - 1]);
    strcpy(g.prod2, prod[g2 - 1]);
    strcpy(g.prod3, prod[g3 - 1]);
    strcpy(g.prod4, prod[g4 - 1]);
    g.price1 = price[g1 - 1];
    g.price2 = price[g2 - 1];
    g.price3 = price[g3 - 1];
    g.price4 = price[g4 - 1];
    g.status = 'd'; //status is set to 'being delivered(d)'
    fseek(fptr, 0, SEEK_END);
    fwrite(&g, sizeof(struct grocery), 1, fptr);
    fflush(stdin);
    printf("\n\n\n\tyour items are being delivered\n\n\n");
    fflush(stdin);
    fclose(fptr);
    return 1;
}
void receipt(int id) {
    float total, mon, change; //mon -> money given by user
    int c = 0;
    fptr = fopen("delivery.txt", "r+");
    if (fptr == NULL) {
        printf("error opening file\n\n");
        exit(0);
    }
    while (fread(&g, sizeof(struct grocery), 1, fptr) == 1) {
        if (g.number == id) {
            printf("\tdelivery total: \n");
            printf("\t%s\t%20.2f\n", g.prod1, g.price1);
            printf("\t%s\t%20.2f\n", g.prod2, g.price2);
            printf("\t%s\t%20.2f\n", g.prod3, g.price3);
            printf("\t%s\t%20.2f\n", g.prod4, g.price4);
            total = g.price1 + g.price2 + g.price3;
            g.ttl = total;
            printf("\t-----------------------------\n");
            printf("\ttotal                   $%.2f\n", total);
            do {
                printf("\tcash :               $");
                scanf("%f", &mon);
                if (mon < total) {
                    c = 0;
                    printf("\n\t insufficient amount...try again\n\n");
                }
                else {
                    g.status = 'r'; //changes status to "received"
                    c = 1;
                    change = mon - total;
                    printf("\tchange:       $%.2f\n", change);
                }

        } while (c == 0); 
        }
        else {
            printf("no delivery pending. \n\n");
            exit(0);
        }
    }
}

int main(){
    int id, a, b, c, d; //a,b,c,d are the items the user wants to buy
    int e = 0; 
    printf("enter USER ID to continue... ");
    scanf("%d", &id);
    printf("ITEMS LIST: \n");
    printf("1 - tissue\n 2 - soap \n 3 - shampoo \n 4 - lotion \n 5 - tampons \n");
    printf("enter item codes: ");
    scanf("%d%d%d%d", &a, &b, &c, &d);
    e = orders(id, a, b, c, d);
    if (e == 1)
        receipt(id);
}

我可以输入任何 ID,它可以很好地打印收据,但我尝试另一个它只是转到 else 语句。

标签: c

解决方案


orders()函数将记录附加到delivery.txt文件中,然后该receipt()函数读取第一条记录并检查记录 id 是否与传递的 id 匹配。如果匹配则继续,否则以"no delivery pending".

如果您希望该receipt()功能查看更多记录,那么您需要以下内容:

void receipt(int id) {
    float total, mon, change; //mon -> money given by user
    int c = 0;
    fptr = fopen("delivery.txt", "r+");
    if (fptr == NULL) {
        printf("error opening file\n\n");
        exit(0);
    }

    int found = 0;
    while (fread(&g, sizeof(struct grocery), 1, fptr) == 1) {
        if (g.number == id) {
            found = 1;
            break;
        }
    }

    if (found) {
        printf("\tdelivery total: \n");
        printf("\t%s\t%20.2f\n", g.prod1, g.price1);
        printf("\t%s\t%20.2f\n", g.prod2, g.price2);
        printf("\t%s\t%20.2f\n", g.prod3, g.price3);
        printf("\t%s\t%20.2f\n", g.prod4, g.price4);
        total = g.price1 + g.price2 + g.price3;
        g.ttl = total;
        printf("\t-----------------------------\n");
        printf("\ttotal                   $%.2f\n", total);
        do {
            printf("\tcash :               $");
            scanf("%f", &mon);
            if (mon < total) {
                c = 0;
                printf("\n\t insufficient amount...try again\n\n");
            }
            else {
                g.status = 'r'; //changes status to "received"
                c = 1;
                change = mon - total;
                printf("\tchange:       $%.2f\n", change);
            }

        } while (c == 0); 
    } else {
        printf("no delivery pending. \n\n");
        exit(0);
    }
}

(请注意,除此之外,代码似乎还有其他问题)。


推荐阅读