在尝试返回c时的问题

时间:2018-01-08 00:28:10

标签: c

我必须为课程编写c语言的代码,但我得到了这个错误,因为代码没有正确地返回值,因为我根本不理解。

要求:

当天游乐园门票每位成人30美元,每名儿童12.00美元。如果总共订购超过10张门票,也可享受10%的折扣。

给定两个整数变量num_adults和num_children,计算当天游乐园入场的总费用,并将其存储在双变量total_cost中。

我的代码:(您可以假设每个变量都已正确初始化。)

if (num_adults + num_children > 10)
{
    total_cost =((30.00 * num_adults) + (12.00 * num_children)) - (0.10 * ((30.00 * num_adults) + (12.00 * num_children)));
    printf("%d\n", total_cost);

}
else 
{
   total_cost=((30.00 * num_adults) + (12.00 * num_children));
   printf("%d\n", total_cost);
}

2 个答案:

答案 0 :(得分:1)

这是我最好的猜测:你想要输出它不是一个整数(%d),而是一个浮动%f

这样做:

printf("%.2f\n", total_cost);

答案 1 :(得分:0)

#include <stdio.h>

int total_tickets(int c, int a)
{
  return (c + a);
}

float total_cost(int c, int a)
{
  return ((12.00 * c) + (30.00 * a));
}

float discounted_cost(int c, int a)
{
  return (total_cost(c, a) * 0.9);
}

float actual_cost(int c, int a)
{
  if(total_tickets(c, a) < 10)
  {
    return (total_cost(c, a));
  }
  else
  {
    return (discounted_cost(c, a));
  }
}

int main(void) {
  int c, a;

  c = 2; a = 2;
  printf("%.2f\n", actual_cost(c, a));

  c = 5; a = 5;
  printf("%.2f\n", actual_cost(c, a));
  return 0;
}