C 只读取第一个结构体,无法读取其他输入

标签 c

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

#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,它会很好地打印收据,但我尝试另一个 ID,它只会转到 else 语句。

最佳答案

orders() 函数将记录附加到 delivery.txt 文件,然后 receipt() 函数读取第一个记录并检查记录 id 是否与传递的 id 匹配。如果匹配,则继续,否则退出,并显示“无待交付”

如果您希望 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);
    }
}

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

关于C 只读取第一个结构体,无法读取其他输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59087447/

相关文章:

ios - 如何在Xcode Project中使用C库?

c - 两个共享库实现相同的 API

c - 将结构指针元素传递给 FIFO - C

c - C 中使用分隔符分割字符串

c - 模式识别 - "is this a pattern?"

c++ - 计算循环中的数字总和

c - 从预处理器中的常量中删除强制转换

c - Gstreamer RTSP `gst-launch-1.0` 等效 C 代码

C(文件处理)

c - 'memory efficient doubly linked list' 是如何工作的?