为了理想生活而奋斗 2023-08-01 14:32 采纳率: 91.4%
浏览 1
已结题

关于##include#的问题,如何解决?

程序不会报错,但在输入第一个数后就没有后续。


#include<stdio.h>
#include<stdlib.h>

struct Date
{
    int year;
    int month;
    int day;
};

struct Book
{
    char title[128];
    char author[40];
    float price;
    struct Date date;
    char publisher[40];
};

void getinput(struct Book *book);
void pb(struct Book *book);
void initlbr(struct Book *lbr[],int *b);
void plbr(struct Book *lbr[],int *b);
void release(struct Book *lbr[],int *b);

void getinput(struct Book *book)
{
    printf("name:");
    scanf("%s",book->title);
    printf("author:");
    scanf("%s",book->author);
    printf("price:");
    scanf("%f",&book->price);
    printf("day:");
    scanf("%d-%d-%d",&book->date.year,&book->date.month,&book->date.day);
    printf("pr:");
    scanf("%s",book->publisher);
}

void pb(struct Book *book)
{
    printf("name:%s\n",book->title);
    printf("author:%s\n",book->author);
    printf("price:%.2f\n",book->price);
    printf("day:%d-%d-%d\n",book->date.year,book->date.month,book->date.day);
    printf("pr:%s\n",book->publisher);
}

void initlbr(struct Book *lbr[],int *b)
{
    int i;
    for(i = 0;i < *b;i++)
    {
        lbr[i] = NULL;
    }
}

void plbr(struct Book *lbr[],int *b)
{
    int i;
    for(i = 0;i < *b;i++)
    {
        if(lbr[i] != NULL)
        {
            pb(lbr[i]);
            putchar('\n');
        }
    }
}

void release(struct Book *lbr[],int *b)
{
    int i;
    for(i = 0;i < *b;i++)
    {
        if(lbr[i] != NULL);
        {
            free(lbr[i]);
        }
    }
}


int main(void)
{
    int *b;
    int c;
    printf("Please set the maxium of books in the lbr:");
    scanf("d",&c);
    b = &c;
    struct Book *lbr[c];
    struct Book *ptr = NULL;
    int a,index = 0;
    
    initlbr(lbr,b);
    
    while(1)
    {
        printf("Do you want to register a book?\n");
        do
        {
            a = getchar();    
        }while(a != 'y' && a != 'n');
        
        if (a == 'y')
        {
            if (index < *b)
            {
                ptr = (struct Book *)malloc(sizeof(struct Book));
                getinput(ptr);
                lbr[index] = ptr;
                index++;
                putchar('\n');
            }
        }
        else
        {
            printf("no more space");
            break;
        }    
    }
    printf("over");
    plbr(lbr,b);
    release(lbr,b);
    
    return 0;
} 
  • 写回答

4条回答 默认 最新

  • 爱编程的小芒果 2023-08-01 14:42
    关注

    scanf输入问题,应该是:

    scanf("%d",&c);
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 8月9日
  • 已采纳回答 8月1日
  • 创建了问题 8月1日