为什么在此代码中使用 fgets 时会跳过用户输入?
Why is user input skipped when using fgets in this code?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
char* title;
float price;
} Book;
void Display(Book[], int);
int main()
{
int n;
printf("How many books would you like to create?\n");
scanf("%d", &n);
Book *books = malloc(n * sizeof(*books));
if (books == NULL)
{
printf("ERROR: Out of memory. \n");
return 1;
}
for (int i = 0; i < n; i++)
{
books[i].title = malloc(50);
if (books[i].title == NULL) {
printf("No memory\n");
return 1;
}
printf("Please enter a title for book %d\n", (i+1));
fgets(books[i].title, 50, stdin);
if ((strlen(books[i].title) > 0) && (books[i].title[strlen (books[i].title) - 1] == '\n'))
books[i].title[strlen (books[i].title) - 1] = '[=10=]';
printf("Please enter a price for book %d\n", (i+1));
scanf("%f", &books[i].price);
}
Display(books, n);
for(int i = 0; i < n; i++)
free(books[i].title);
free(books);
return 0;
}
void Display(Book list[], int size)
{
for (int i = 0; i < size; i++)
{
printf("Title: %s, price: %.2f$. \n", list[i].title, list[i].price);
}
}
大家好,我正在尝试学习 C,但我一直被这个问题困扰了一段时间。我正在尝试制作一个程序,询问用户他想要多少本书,然后继续询问每本书的标题(包括空格)和价格。每当我 运行 此代码时,结果如下:
How many books would you like to create?
2
Please enter a title for book 1
Please enter a price for book 1
45.4
Please enter a title for book 2
Please enter a price for book 2
245.6
Title: , price: 45.40$.
Title: , price: 245.60$.
为什么标题输入被跳过?
你的问题是这个电话:
scanf("%d", &n);
一旦 scanf()
完成对数字的解析,它会将未使用的输入留在缓冲区中。所以用户在数字后输入的换行符是你的:
fgets(books[i].title, 50, stdin);
作为输入。一个空行。如果您所有的输入都只有 fgets()
或只有 scanf()
,那么可能有一种干净的方法来处理这个问题。但是,为了让您在开发中取得进展,您可以这样做:
scanf("%d%*c", &n);
scanf("%f%*c", &books[i].price);
哪个会为你吃掉换行符。但是,这不是一个完整的修复,因为用户可以在数字后键入一个额外的 space,然后再键入 return,您又回到了同一条船上。只是一个让你前进的快速修复,然后你可以在对 Google.
进行一些研究后返回并正确解决这个问题
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
char* title;
float price;
} Book;
void Display(Book[], int);
int main()
{
int n;
printf("How many books would you like to create?\n");
scanf("%d", &n);
Book *books = malloc(n * sizeof(*books));
if (books == NULL)
{
printf("ERROR: Out of memory. \n");
return 1;
}
for (int i = 0; i < n; i++)
{
books[i].title = malloc(50);
if (books[i].title == NULL) {
printf("No memory\n");
return 1;
}
printf("Please enter a title for book %d\n", (i+1));
fgets(books[i].title, 50, stdin);
if ((strlen(books[i].title) > 0) && (books[i].title[strlen (books[i].title) - 1] == '\n'))
books[i].title[strlen (books[i].title) - 1] = '[=10=]';
printf("Please enter a price for book %d\n", (i+1));
scanf("%f", &books[i].price);
}
Display(books, n);
for(int i = 0; i < n; i++)
free(books[i].title);
free(books);
return 0;
}
void Display(Book list[], int size)
{
for (int i = 0; i < size; i++)
{
printf("Title: %s, price: %.2f$. \n", list[i].title, list[i].price);
}
}
大家好,我正在尝试学习 C,但我一直被这个问题困扰了一段时间。我正在尝试制作一个程序,询问用户他想要多少本书,然后继续询问每本书的标题(包括空格)和价格。每当我 运行 此代码时,结果如下:
How many books would you like to create?
2
Please enter a title for book 1
Please enter a price for book 1
45.4
Please enter a title for book 2
Please enter a price for book 2
245.6
Title: , price: 45.40$.
Title: , price: 245.60$.
为什么标题输入被跳过?
你的问题是这个电话:
scanf("%d", &n);
一旦 scanf()
完成对数字的解析,它会将未使用的输入留在缓冲区中。所以用户在数字后输入的换行符是你的:
fgets(books[i].title, 50, stdin);
作为输入。一个空行。如果您所有的输入都只有 fgets()
或只有 scanf()
,那么可能有一种干净的方法来处理这个问题。但是,为了让您在开发中取得进展,您可以这样做:
scanf("%d%*c", &n);
scanf("%f%*c", &books[i].price);
哪个会为你吃掉换行符。但是,这不是一个完整的修复,因为用户可以在数字后键入一个额外的 space,然后再键入 return,您又回到了同一条船上。只是一个让你前进的快速修复,然后你可以在对 Google.
进行一些研究后返回并正确解决这个问题