【问题标题】:Why is user input skipped when using fgets in this code? [duplicate]为什么在此代码中使用 fgets 时会跳过用户输入? [复制]
【发布时间】:2020-09-26 21:18:51
【问题描述】:
#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] = '\0';

        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$.

为什么标题输入会被跳过?

【问题讨论】:

  • 不要只是malloc任意数量的内存,分配你需要的

标签: c arrays string whitespace fgets


【解决方案1】:

你的问题是这个电话:

scanf("%d", &n);

一旦scanf() 完成对数字的解析,它会将未使用的输入留在缓冲区中。所以用户在数字后面输入的换行符就是你的:

fgets(books[i].title, 50, stdin);

作为输入。一个空行。如果您的所有输入都只有fgets() 或只有scanf(),那么可能有一种干净的方法来处理这个问题。但是,为了让您在开发中继续前进,您可以这样做:

scanf("%d%*c", &n);
scanf("%f%*c", &books[i].price);

哪个会为你吃掉换行符。但是,这不是一个完整的解决方案,因为用户可以在数字后键入一个额外的空格,然后返回,而您又回到了同一条船上。只需快速修复即可让您继续前进,然后您可以在对 Google 进行一些研究后返回并正确解决此问题。

【讨论】:

  • 感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2014-12-06
  • 1970-01-01
  • 2021-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-14
  • 2021-08-11
相关资源
最近更新 更多