returns 书名、价格和页数的程序

program which returns name,price and number of pages of book

这只是一个基本程序。我们将输入书名(1 个字符名称)、价格(浮点数)和页数(int)ant 输出应该与输入相同,但事实并非如此。

#include<stdio.h>

int main()
{
    struct book
    {
        char name;
        float price;
        int pages;
    };

    struct book b1,b2,b3;
    printf("Enter names,prices & no. of pages of 3 books\n");
    scanf("%c %f %d",&b1.name,&b1.price,&b1.pages);
    scanf("%c %f %d",&b2.name,&b2.price,&b2.pages);
    scanf("%c %f %d",&b3.name,&b3.price,&b3.pages);
    printf("And this is what you entered\n");
    printf("%c %f %d\n",b1.name,b1.price,b1.pages);
    printf("%c %f %d\n",b2.name,b2.price,b2.pages);
    printf("%c %f %d\n",b3.name,b3.price,b3.pages);
    return 0;
}

这就是您输入的内容。浮点数更多的是近似值而不是实际值。这就是为什么当您输入 123.134 时,计算机将其四舍五入为最接近的可能值 123.124003。它适用于工程、图形和统计,这些微小的差异在呈现给人类之前会被四舍五入。但是当你想要精确的值时,浮点变量是完全不适合的。

在财务中,我们将 10 美元存储为“1000 美分”,以整数表示。尝试对货币值使用 float 或 double 是一个很大的禁忌。

你要的是小数类型,但是C中没有这种类型

您使用的程序有误。

"%c %f %d" 表示:scan 1 character, then a space, then a float, then a space, then an int。没有回车。我 运行 你的程序运行正常 - 只要我不按 Enter:

Enter names,prices & no. of pages of 3 books                                                                                                                                       
a 123.33 555b 124.44 666c125.555 777                                                                                                                                               
And this is what you entered                                                                                                                                                       
a 123.330002 555                                                                                                                                                                   
b 124.440002 666                                                                                                                                                                   
c 125.555000 777

您可以看到 scanf 如何检测最后 %d 到第一个 %c 之间的变化,只需计算出 "hmm, 'b' doesn't fit as another digit of '555' so let's finish the %d." 然后下一行是 运行。如果您按 Enter,则换行符 (Enter) 会转到下一本书的标题。然后当你按下 'b' 时,它既不适合 %f 也不适合 %d,所以它们被设置为零,然后你的 'b' 最终被匹配为第三本书的标题。所以你有 3 本书:a、Enter 和 b。

如果你想要求输入然后添加 "expect Enter": scanf("%c %f %d\n" 现在你的程序是这样运行的:

Enter names,prices & no. of pages of 3 books                                                                                                                                       
a 123.33 555                                                                                                                                                                       
b 124.44 666                                                                                                                                                                       
c 125.55 777                                                                                                                                                                       
And this is what you entered                                                                                                                                                       
a 123.330002 555                                                                                                                                                                   
b 124.440002 666                                                                                                                                                                   
c 125.550003 9  

scanf 只是 完全按照您的要求 做。

不幸的是,两个链接的答案都不是很好。问题不在第二行的开头,而是在第一行的开头。