C 为什么我的程序会跳过第一个fgets()?

C 为什么我的程序会跳过第一个fgets()?,c,string-parsing,C,String Parsing,这是一个选择: printf("\t1. Add new book record"); printf("\n"); printf("\t2. Edit book title"); printf("\n"); printf("\t3. View all the books"); printf("\n"); printf("\t4. View a specific book"); printf("\n"); printf("\t5. Delete a book"); printf("\n"); p

这是一个选择:

printf("\t1. Add new book record");
printf("\n");
printf("\t2. Edit book title");
printf("\n");
printf("\t3. View all the books");
printf("\n");
printf("\t4. View a specific book");
printf("\n");
printf("\t5. Delete a book");
printf("\n");

printf("Your choice:\t");
scanf("%d", &ch);
printf("\n");
然后根据用户的输入转到另一个函数。除了增加一个新的图书记录,其他一切都很好。该程序跳过第一个fgets(),要求用户输入书的编号,而直接转到第二个fgets(),这可以正常工作

以下是我获取信息的代码:

void getInfo(BOOK *data)
{
    printf("Enter Book Number: ");
    fgets((*data).bkNum,M, stdin);
    printf("Enter Book Title: ");
    fgets((*data).bkTitle, M, stdin);
    printf("Enter Book Author: ");
    fgets((*data).bkAuthor, M, stdin);
    printf("Enter Book Copyright: ");
    scanf("%d", &(*data).bkCopyright);
    return;
}

您的
scanf
调用在输入缓冲区中留下一个换行符,然后第一次
fgets
调用会使用它。在调用
fgets
之前,您需要吃掉该角色