这是选择:
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
之前吃掉该角色。
关于c - 为什么我的程序跳过第一个 fgets()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29057542/