我输入的字符串值未显示

问题描述 投票:0回答:1

我正在尝试创建一个 C 程序来显示最昂贵和最便宜的书籍(使用结构数据类型)。 但是,当我输入所有值时,仅显示浮点值(图书价格),而不显示字符串(标题和作者)。 我可以采取什么步骤来解决这个问题?

// Exercise N3 - Resource
// Structure.
#include <stdio.h>
#include <string.h>

int main()
{
    
    // Defining Structure.
    struct Book
    {
        char title[40];
        char author[40];
        float price;
    };
    
    struct Book book_1;
    struct Book book_2;
    struct Book book_3;
    
    // Variables Declarations.
    // Needed to deal with string values.
    
    char title_1[40];
    char title_2[40];
    char title_3[40];
    
    char author_1[40];
    char author_2[40];
    char author_3[40];
    
    
    printf("Hey User !\n");
    
    // First Book Information.
    printf("Enter first book's information\n");
    
    printf("Book's Title :\n");
    fgets(title_1,40,stdin);
    title_1[strlen(title_1) - 1] = '\0';
    strcpy(title_1,book_1.title);
    printf("%s",book_1.title);
    
    printf("Book's Author :\n");
    fgets(author_1,40,stdin);
    author_1[strlen(author_1) - 1] = '\0';
    strcpy(author_1,book_1.author);
    
    printf("Book's Price :\n");
    scanf("%f",&book_1.price);
    fflush(stdin);
    
    printf("\n");
    printf("\n");
    
    // Second Book Information.
    printf("Enter second book's information\n");
    
    printf("Book's Title :\n");
    fgets(title_2,40,stdin);
    title_2[strlen(title_2) - 1] = '\0';
    strcpy(title_2,book_2.title);
    
    printf("Book's Author :\n");
    fgets(author_2,40,stdin);
    author_2[strlen(author_2) - 1] = '\0';
    strcpy(author_2,book_2.author);
    
    printf("Book's Price :\n");
    scanf("%f",&book_2.price);
    fflush(stdin);
    
    printf("\n");
    printf("\n");
    
    // Third Book Information.
    printf("Enter third book's information\n");
    
    printf("Book's Title :\n");
    fgets(title_3,40,stdin);
    title_3[strlen(title_3) - 1] = '\0';
    strcpy(title_3,book_3.title);
    
    printf("Book's Author :\n");
    fgets(author_3,40,stdin);
    author_3[strlen(author_3) - 1] = '\0';
    strcpy(author_3,book_3.author);
    
    printf("Book's Price :\n");
    scanf("%f",&book_3.price);
    fflush(stdin);
    
    printf("\n");
    printf("\n");
    
    
    if (book_1.price > book_2.price && book_1.price > book_3.price)
    {
        printf("Title : %s\n",book_1.title);
        printf("Author : %s\n",book_1.author);
        printf("Price : %0.2f €\n",book_1.price);
    }
    else if (book_2.price > book_1.price && book_2.price > book_3.price)
    {
        printf("Title : %s\n",book_2.title);
        printf("Author : %s\n",book_2.author);
        printf("Price : %0.2f €\n",book_2.price);
    }
    else if (book_3.price > book_1.price && book_3.price > book_2.price)
    {
        printf("Title : %s\n",book_3.title);
        printf("Author : %s\n",book_3.author);
        printf("Price : %0.2f €\n",book_3.price);
    }
    
    printf("\n");
    printf("\n");
    
    if (book_1.price < book_2.price && book_1.price < book_3.price)
    {
        printf("Title : %s\n",book_1.title);
        printf("Author : %s\n",book_1.author);
        printf("Price : %0.2f €\n",book_1.price);
    }
    else if (book_2.price < book_1.price && book_2.price < book_3.price)
    {
        printf("Title : %s\n",book_2.title);
        printf("Author : %s\n",book_2.author);
        printf("Price : %0.2f €\n",book_2.price);
    }
    else if (book_3.price < book_1.price && book_3.price < book_2.price)
    {
        printf("Title : %s\n",book_3.title);
        printf("Author : %s\n",book_3.author);
        printf("Price : %0.2f €\n",book_3.price);
    }
    
    
    return 0;
}

我尝试更改字符数组的大小,但无法解决问题。

c string struct scanf fgets
1个回答
0
投票

您的基本错误很简单,并在评论中指出。您对

strcpy
的论证顺序错误。首先是目的地,然后是源。

但是,根本没有必要使用

strcpy
。您可以直接阅读每本书的字符数组。

您还可以使用书籍数组而不是三个单独的变量。然后,您只需要定义一个比较器,就可以对数组进行排序,从而为您提供最昂贵和最便宜的书籍。当然,这优于复制和粘贴代码。

#include <stdio.h>
#include <string.h>

struct Book {
    char title[40];
    char author[40];
    float price;
};

int compare_books_by_price(void *a, void *b) {
    struct Book *c = (struct Book *)a;
    struct Book *d = (struct Book *)b;

    if (c->price == d->price) return 0;
    if (c->price < d->price) return -1;
    return 1;
}

#define NUM_BOOKS 3

int main(void) {
    struct Book books[NUM_BOOKS];

    printf("Hey User !\n");

    for (size_t i = 0; i < NUM_BOOKS; i++) {
        printf("Enter #%d book's information\n", i+1);

        printf("Book's Title :\n");
        fgets(books[i].title, 40, stdin);
        books[i].title[strlen(books[i].title) - 1] = '\0';
    
        printf("Book's Author :\n");
        fgets(books[i].author, 40, stdin);
        books[i].author[strlen(books[i].author) - 1] = '\0';
    
        printf("Book's Price :\n");
        scanf("%f", &books[i].price);
        fflush(stdin);
    } 

    qsort(books, NUM_BOOKS, sizeof(struct Book), compare_books_by_price);

    // books[0] is now the least expensive
    // books[NUM_BOOKS - 1] is now the most expensive
}

您还应该检查您的输入是否成功,并适当处理的情况。

© www.soinside.com 2019 - 2024. All rights reserved.