【问题标题】:Float output is incorrect浮点输出不正确
【发布时间】:2013-03-16 11:28:03
【问题描述】:

我创建了一个程序,它为用户输入一个输入,然后让他们输入他们想要输入的金额,但是当我打印总和金额时。然而,这是全面返回相同的值:即

总计:5.00 食物:5.00 账单:5.00 旅行:5.00 同性恋:5.00

代替:

总计:14.00 食物:2.00 账单:3.00 旅行:4.00 同性恋:5.00

int main(void)
{

float food = 0.00;
float travel = 0.00;
float bills = 0.00;
float fags = 0.00;
float total = 0.00;

float t_food, t_travel, t_bills, t_fags;

char userInput[3];

while(userInput[0] != 'X')
{

    printf("Select option,\nA: Food\nB: Travel\nC: Bills\nD: Fags\nX: Exit\n");
    scanf("%s", userInput);
    if((userInput[0] == 'A') || (userInput[0] =='a'))
    {
        printf("Please enter an amount: ");
        scanf("%f", &food);
        printf("You have entered: %.2f\n", food);
        t_food += food;

    }
    if((userInput[0] == 'B') || (userInput[0] =='b'))
    {
        printf("Please enter an amount: ");
        scanf("%f", &travel);
        printf("You have entered: %.2f\n", travel);
        t_travel += travel;

    }
    if((userInput[0] == 'C') || (userInput[0] =='c'))
    {
        printf("Please enter an amount: ");
        scanf("%f", &bills);
        printf("You have entered: %.2f\n", bills);
        t_bills += bills;

    }
    if((userInput[0] == 'D') || (userInput[0] =='d'))
    {
        printf("Please enter an amount: ");
        scanf("%f", &fags);
        printf("You have entered: %.2f\n", fags);
        t_fags += fags;

    }
    if((userInput[0] =='X') || (userInput[0] =='x'))
    {
        total = t_food + t_fags + t_travel + t_bills;

        printf("Total: %.2f\n", &total);
        printf("Food: %.2f\n", &t_food);
        printf("Travel: %.2f\n", &t_travel);
        printf("Bills: %.2f\n", &t_bills);
        printf("Fags: %.2f\n", &t_fags);
        break;
    }
}
return 0;

}

有什么想法吗?

【问题讨论】:

  • 可能会分解一下以隔离,因为它是写我想关闭它,因为它依赖于用户输入......所以将你的问题隔离到逻辑、输入或输出。
  • 大声笑,哦,是的,我错过了 printfs 中的 & 符号......仍然不要在没有减少问题的情况下发布问题

标签: c math input floating-point output


【解决方案1】:

改变

    printf("Total: %.2f\n", &total);
    printf("Food: %.2f\n", &t_food);
    printf("Travel: %.2f\n", &t_travel);
    printf("Bills: %.2f\n", &t_bills);
    printf("Fags: %.2f\n", &t_fags);

    printf("Total: %.2f\n", total);
    printf("Food: %.2f\n", t_food);
    printf("Travel: %.2f\n", t_travel);
    printf("Bills: %.2f\n", t_bills);
    printf("Fags: %.2f\n", t_fags); 

听编译器说,

warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘float *’ [-Wformat]

【讨论】:

    【解决方案2】:

    & 放到printf 中,表示您传递的是值的位置,而不是值本身。

    【讨论】:

      【解决方案3】:

      您应该使用printf 中的值,而不是地址。

      【讨论】:

        【解决方案4】:

        如果您在最后一个 if 代码块中删除 &,一切都会正常

        【讨论】:

          【解决方案5】:

          除了前面关于 printf 的答案外,您还需要初始化包含总值的浮点变量。比如:

          float t_food=0, t_travel=0, t_bills=0, t_fags=0;
          ...
          printf("Total: %.2f\n", total);
          printf("Food: %.2f\n", t_food);
          printf("Travel: %.2f\n", t_travel);
          printf("Bills: %.2f\n", t_bills);
          printf("Fags: %.2f\n", t_fags);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-05-02
            • 2012-02-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多