繁体   English   中英

printf 不打印变量浮点答案

[英]printf doesn't print variable float answer

尝试在添加税款和小费后计算账单总额,然后将其分摊给 2 个人; 代码编译但不打印最终值。

#include <cs50.h>
#include <stdio.h>

float half(float bill, float tax, int tip);

int main(void)
{
    float bill_amount = get_float("Bill before tax and tip: ");
    float tax_percent = get_float("Sale Tax Percent: ");
    int tip_percent = get_int("Tip percent: ");

{

        float tax = tax_percent/100.0;

        float bill = bill_amount + tax;

        int tip =  tip_percent / 100;

        float total = tip + bill + tax;

        return total / 2;

}
        printf("You will owe $%.2f each!\n", half(bill_amount, tax_percent, tip_percent));
}

这是 cs50 课程的一部分 - 附带说明,我发现它在运行代码时非常困难,但通常它是不正确的,在我搜索它之前我看不到解决方案。 即便如此,理解它本身仍然是一项壮举; 像这里为什么我要使用返回? 我知道它是必需的,因为它已为我们加载,但我不明白它的用途? 在 printf 中,它在逗号后使用了一半,但它如何知道该值应该是多少? 我试过这样做:

float tax = tax_percent/100.0;

float bill = bill_amount + tax;

int tip =  tip_percent / 100;

float half = tip + bill + tax/2;

但这在编译时有错误,因为我认为它不像我将 half 声明为浮点数。 我不确定我应该了解多少,但是 cs50 做出了很多我以前从未做过编程时就知道的假设; 无论如何,非常感谢您的帮助-谢谢

看起来这里有half的 function原型float half(float bill, float tax, int tip); ,但从未定义 function。 程序遇到return语句从main function返回,程序结束。

在main function的结束}之后添加function定义和主体。这在示例代码中的mario8.c中进行了演示,其讨论从视频的1:42:05开始。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM