Печать данных из списка с условием

Мне нужно сделать функцию, которая печатает данные только за определенный месяц и год (см. case 2И void printList(LIST*pFirst, int pm, int py)), но кажется, что я делаю что-то не так, как вся программа аварийно завершает работу:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
#define MAX_LENGTH 80
#define LOOPS 1
struct delivery
{
    int num;
    char name[20];
    char code[8];
    char info[80];
    int day;
    int month;
    int year;
    int amount;
};
typedef struct delivery BODY;
struct List{
    BODY body;
    struct List*pNext;
};
typedef struct List LIST;
int enterBody(BODY*ps);
void printBody(BODY s);
void printList(LIST*pFirst, int pm, int py);
LIST*insertBegin(LIST*pFirst, BODY newBody);

int main()
{
    FILE*pOut = NULL, *pIn = NULL;
    char Fname[] = "List_bin.dat";
    BODY newbody;
    LIST*pFirst = NULL, *p;
    int res, i, mode, pm, py;
    char sfn[20], gr[20];
    int newgr, fn;
    BODY delivery;
    char*menu[] = { "MENU:",
        "1 - Enter info for new delivery ",
        "2 - Print info for deliveries for an entered month ",
    };
    do
    {
        system("cls");
        for (i = 0; i < 3; i++)
            printf("n%sn", menu[i]);
        do
        {
            fflush(stdin);
            printf("nChoose operation (1-2): ");
            res = scanf("%d", &mode);
        } while (res != 1);
        switch (mode)
        {
        case 1:
            for (i = 0; i < LOOPS; i++)
            {
                res = enterBody(&delivery);
                if (res != 1)
                {
                    printf("Error in initialization %dn", res);
                    break;
                }
                p = insertBegin(pFirst, delivery);
                if (p == NULL)
                {
                    printf("Not enough memory! ");
                    break;
                }
                pFirst = p;
            }
            system("pause");
            break;
        case 2:
            printf("nEnter month: ");
            scanf("%19s", pm);
            printf("nEnter year: ");
            scanf("%19s", py);
            if (pFirst != NULL)
            {
                printList(pFirst, pm, py);
            }
            else printf("nEmpty list!n");
            system("pause");
            break;

        default:
            printf("nIncorrect operation!n");
            system("pause");
        }
    } while (mode != 11);
    system("pause");
    return 0;
}

int enterBody(BODY*ps)
{
    if (ps == NULL) return 0;
    memset(ps, 0, sizeof(BODY));
    fflush(stdin);
    printf("nEnter delivery number: ");
    scanf("%d", &(ps->num));
    fflush(stdin);
    printf("nEnter name of product: ");
    gets(ps->name);
    printf("nEnter provider code: ");
    gets(ps->code);
    printf("nEnter provider's company, address and phone number: ");
    gets(ps->info);
    printf("nEnter date: ");
    scanf("%d", &(ps->day));
    fflush(stdin);
    printf("nEnter month: ");
    scanf("%d", &(ps->month));
    fflush(stdin);
    printf("nEnter year: ");
    scanf("%d", &(ps->year));
    fflush(stdin);
    printf("nEnter amount delivered: ");
    scanf("%d", &(ps->amount));
    fflush(stdin);
    return 1;
}

void printBody(BODY s)
{
    printf("nNumber: %dtName: %stCode: %stInfo: %snDate: %d-%d-%dtAmount: %d",
        s.num, s.name, s.code, s.info, s.day, s.month, s.year, s.amount);;
}

void printList(LIST*pFirst, int pm, int py)
{
    LIST*p = NULL;

        if(strcmp(p->body.month, pm) == 0)
        p = pFirst;
        while (p != NULL)
        {
            printBody(p->body);
            p = p->pNext;
        }
        printf("n");
}

LIST*insertBegin(LIST*pFirst, BODY newBody)
{
    LIST*p;
    p = (LIST*)malloc(sizeof(LIST));
    if (p == NULL)
    {
        printf("Not enough memoryn");
        return NULL;
    }
    else
    {
        p->body = newBody;
        p->pNext = pFirst;
        pFirst = p;
        return p;
    }
}

1 ответ

  1. Вам нужно всего 2 небольших изменения, чтобы сделать программу не аварийно завершить работу для случая 2. Вы сканируете неправильный тип для цифры, вы должны использовать %dи указатель на int. А при печати списка нужно начинать с самого начала.

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<malloc.h>
    #define MAX_LENGTH 80
    #define LOOPS 1
    struct delivery
    {
        int num;
        char name[20];
        char code[8];
        char info[80];
        int day;
        int month;
        int year;
        int amount;
    };
    typedef struct delivery BODY;
    struct List{
        BODY body;
        struct List*pNext;
    };
    typedef struct List LIST;
    int enterBody(BODY*ps);
    void printBody(BODY s);
    void printList(LIST*pFirst, int pm, int py);
    LIST*insertBegin(LIST*pFirst, BODY newBody);
    
    int main()
    {
        FILE*pOut = NULL, *pIn = NULL;
        char Fname[] = "List_bin.dat";
        BODY newbody;
        LIST*pFirst = NULL, *p;
        int res, i, mode, pm, py;
        char sfn[20], gr[20];
        int newgr, fn;
        BODY delivery;
        char*menu[] = { "MENU:",
                        "1 - Enter info for new delivery ",
                        "2 - Print info for deliveries for an entered month ",
        };
        do
        {
            system("cls");
            for (i = 0; i < 3; i++)
                printf("\n%s\n", menu[i]);
            do
            {
                fflush(stdin);
                printf("\nChoose operation (1-2): ");
                res = scanf("%d", &mode);
            } while (res != 1);
            switch (mode)
            {
                case 1:
                    for (i = 0; i < LOOPS; i++)
                    {
                        res = enterBody(&delivery);
                        if (res != 1)
                        {
                            printf("Error in initialization %d\n", res);
                            break;
                        }
                        p = insertBegin(pFirst, delivery);
                        if (p == NULL)
                        {
                            printf("Not enough memory! ");
                            break;
                        }
                        pFirst = p;
                    }
                    system("pause");
                    break;
                case 2:
                    printf("\nEnter month: ");
                    scanf("%d", &pm);
                    printf("\nEnter year: ");
                    scanf("%d", &py);
                    if (pFirst != NULL)
                    {
                        printList(pFirst, pm, py);
                    }
                    else printf("\nEmpty list!\n");
                    system("pause");
                    break;
    
                default:
                    printf("\nIncorrect operation!\n");
                    system("pause");
            }
        } while (mode != 11);
        system("pause");
        return 0;
    }
    
    int enterBody(BODY*ps)
    {
        if (ps == NULL) return 0;
        memset(ps, 0, sizeof(BODY));
        fflush(stdin);
        printf("\nEnter delivery number: ");
        scanf("%d", &(ps->num));
        fflush(stdin);
        printf("\nEnter name of product: ");
        gets(ps->name);
        printf("\nEnter provider code: ");
        gets(ps->code);
        printf("\nEnter provider's company, address and phone number: ");
        gets(ps->info);
        printf("\nEnter date: ");
        scanf("%d", &(ps->day));
        fflush(stdin);
        printf("\nEnter month: ");
        scanf("%d", &(ps->month));
        fflush(stdin);
        printf("\nEnter year: ");
        scanf("%d", &(ps->year));
        fflush(stdin);
        printf("\nEnter amount delivered: ");
        scanf("%d", &(ps->amount));
        fflush(stdin);
        return 1;
    }
    
    void printBody(BODY s)
    {
        printf("\nNumber: %d\tName: %s\tCode: %s\tInfo: %s\nDate: %d-%d-%d\tAmount: %d",
               s.num, s.name, s.code, s.info, s.day, s.month, s.year, s.amount);;
    }
    
    void printList(LIST*pFirst, int pm, int py)
    {
        LIST*p = NULL;
    
        p = pFirst;
        while (p != NULL)
        {
            printBody(p->body);
            p = p->pNext;
        }
        printf("\n");
    }
    
    LIST*insertBegin(LIST*pFirst, BODY newBody)
    {
        LIST*p;
        p = (LIST*)malloc(sizeof(LIST));
        if (p == NULL)
        {
            printf("Not enough memory\n");
            return NULL;
        }
        else
        {
            p->body = newBody;
            p->pNext = pFirst;
            pFirst = p;
            return p;
        }
    }
    

    Тест

    MENU:
    
    1 - Enter info for new delivery 
    
    2 - Print info for deliveries for an entered month 
    
    Choose operation (1-2): 1
    
    Enter delivery number: 2
    
    Enter name of product: 
    Enter provider code: 2
    
    Enter provider's company, address and phone number: 2
    
    Enter date: 2
    
    Enter month: 2
    
    Enter year: 2
    
    Enter amount delivered: 2
    sh: 1: pause: not found
    sh: 1: cls: not found
    
    MENU:
    
    1 - Enter info for new delivery 
    
    2 - Print info for deliveries for an entered month 
    
    Choose operation (1-2): 2
    
    Enter month: 2
    
    Enter year: 2
    
    Number: 2   Name:   Code: 2 Info: 2
    Date: 2-2-2 Amount: 2