c - 如何让每个 scanf 工作而不跳过
问题描述
我用gets和scanf做循环。实际上我为换行缓冲区腾出了空间。所以我认为每个 scanf 都有效,但是当我向 &books[ctr].cost 编译器输入值时,会跳过接下来的 3 个问题。我不知道为什么它这样工作
书籍是结构变量
for (ctr = 0 ; ctr < 3 ; ctr++)
{
printf("What is the name of the book #%d?\n", (ctr+1));
gets(books[ctr].title);
puts("Who is the author? ");
gets(books[ctr]. author);
puts("How much did the book cost? ");
scanf(" $%f", &books[ctr].price);
puts("How many pages in the book? ");
scanf(" %d", &books[ctr].pages);
getchar( );
}
printf("\n\n Here is the collection of books : \n");
for (ctr = 0 ; ctr < 3 ; ctr ++)
{
printf("#%d: %s by %s", (ctr+1), books[ctr].title, books[ctr].author);
printf("\n It is %d pages and costs $%.2f", books[ctr].pages, books[ctr].price);
printf("\n\n");
}
return 0;
}
解决方案
推荐阅读
- c - 如何在 C 中查找错误
- ruby-on-rails - Simpleform Append 仅适用于某些输入类型
- bouncycastle - java - 如何使用现有的公钥进行 PGP 加密?
- python - 将输入映射到字典值
- pandas - pandas 数据框以 3D 方式绘制多个帧
- python - 将数据从 django 模型传递到列表中
- mysql - 使用变量时如何在查询中使用 GROUP BY
- python - 这是一个 distutils 安装的项目,因此我们无法准确确定哪些文件属于它,这只会导致部分卸载
- javascript - Scrapy:从脚本标签中提取数据
- selenium - Jenkins - 显示失败测试的名称 TestNG