【问题标题】:Mixing types in function declaration multifunction programs函数声明多功能程序中的混合类型
【发布时间】:2016-03-04 03:37:29
【问题描述】:

我无法让我的程序在计算面积后实际计算任何值。

它计算得很好,我为自己没有错误感到自豪。

但现在它只是在我的 scanf 语句要求加倍之后吐出空白行并要求更多。为什么?

我编写了一个 printf 来取回我的 scanf 值,只是为了检查编译器是否得到它。它不是。没看懂。

#include <stdio.h>

int main (void)
{ int length;
  int width;
  int area;
  double costPersqft;

//Statements
  printf("Enter length in feet: ");
  scanf("%d", &length);
  printf("Enter width in feet: ");
  scanf("%d", &width);

  int get_Area (int x, int y)
  {
  return(x*y);
  }//Area

  area = get_Area (length, width);

  printf("Length: %d\n", length);
  printf("Width: %d\n", width);    
  printf("Area: %d\n", area);

  printf("Enter cost per square foot: ");
  scanf("%f", &costPersqft);
  printf("%.2f", costPersqft);

  return 0;
}//Main

【问题讨论】:

  • 也许尝试使用%lf 而不是%f
  • 将指向具有错误类型的对象的指针传递给scanf() 会调用未定义的行为
  • “它计算得很好,我为自己没有错误感到自豪。”当心!编译不代表没有bug。
  • 请注意,C 中的函数嵌套是 GCC 扩展,不在标准中。在这种情况下你不需要它。
  • “在我的 scanf 声明之后要求更多”。这到底是什么意思?请显示确切的测试运行日志 - 将其作为文本粘贴到问题中。但对于初学者scanf("%f", &amp;costPersqft);%f 指令需要一个指向 float 的指针,但您给它的是一个指向 double 的指针。将costPersqft 的类型更改为float(或将%f 更改为%lf 以处理double)。

标签: c


【解决方案1】:

正如 cmets 中所说,您的主要问题是在您的 scanf 中使用了错误的格式说明符。 double 的正确格式说明符是 %lf,而不是 %f。使用错误的格式说明符会调用未定义的行为。

所以改变

scanf("%f", &costPersqft);

scanf("%lf", &costPersqft);

您还应该检查所有scanfs 的返回值。在您的情况下,如果成功,它们都将返回 1。


如果您启用它们,编译器会向您显示警告。使用-Wall -Wextra 启用警告。注意警告,不要忽视它们。


另外,Nested functions are a GCC extenion 在您的问题下方的 cmets 中为 said by @MikeCAT。这不是标准的C。所以我建议移动

int get_Area (int x, int y)
{
  return(x * y);
}

就在main的定义之前。

【讨论】:

    【解决方案2】:

    底部混乱,但这些结果直接来自我的程序。

    #include <stdio.h>
    #define tax 0.085
    #define fixedRate .35
    
    int main (void)
    { int length;
      int width;
      int area;
      double costPersqft;
      double carpet_charge;
      double labor;
      double installed_price;
      double actual_discount;
      double subtotal;
      double discount;
      double tax_amount;
      double total;
    
    //Statements
      printf("Enter length in feet: ");
      scanf("%d", &length);
      printf("Enter width in feet: ");
      scanf("%d", &width);
      printf("Enter customer discount (xx.xx): ");
      scanf("%lf", &discount);
      printf("Enter cost per square foot(xxx.xx): ");
      scanf("%lf", &costPersqft);
    
      int get_Area (int x, int y)
      {
      return(x*y);
      }//Area
    area = get_Area (length, width);
      printf("               MEASUREMENT        \n");
      printf("Length:                     %d ft \n", length);
      printf("Width:                      %d ft \n", width);
      printf("Area:                       %d square ft \n",
    area);
    
      double get_Cacharge (int p, double q)
      { double product;
      product = p * q;
      return product;
      }//Area times costpersqft is charges for carpet
    
      carpet_charge = get_Cacharge (area, costPersqft);
      printf("                CHARGES              \n");
      printf("DESCRIPTION   COST/SQ.FT.      CHARGE\n");
      printf("-----------   -----------   -----------\n");
      printf("Carpet Cost      %5.2lf        $%8.2lf\n", costPersqft, carpet_charge);
    
      double get_Laborcost (int r)
      { double labor = 0.35;
        double product;
        product = r * labor;
        return product;
      }//labor cost
    
      labor = get_Laborcost (area);
    
      printf("Labor            %5.2lf         %8.2lf\n", fixedRate, labor);
      printf("                            -----------\n");
      installed_price = labor + carpet_charge;
    printf("Installed Price               $%8.2lf\n", installed_price);
    
      double get_Discount (double c, double d)
      { double temp;
        temp = c * d;
        return temp;
      }
    
      actual_discount = get_Discount (installed_price, discount);
    
      printf("Discount         %5.2lf         %8.2lf\n", discount, actual_discount);
      printf("                            -----------\n");
      subtotal = installed_price - actual_discount;
    
      printf("Subtotal                      $%8.2lf\n", subtotal);
    
      tax_amount = tax * subtotal;
    printf("Tax                            %8.2lf\n", tax_amount);
    
      total = subtotal + tax_amount;
    
     printf("Total                         $%8.2lf\n", total);
    
      return 0;
    }//Main
    

    以英尺为单位输入长度:14 以英尺为单位输入宽度:11 输入客户折扣 (xx.xx): 00.10 输入每平方英尺成本(xxx.xx):022.25 测量
    长度:14 英尺 宽度:11 英尺 面积:154平方英尺 收费
    说明 成本/平方英尺。收费 ------------ ------------ ------------ 地毯成本 22.25 $ 3426.50 劳动力 0.35 53.90 ------------ 安装价格 $3480.40 折扣 0.10 348.04 ------------ 小计 $3132.36 税 266.25 总计 3398.61 美元

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-15
      • 2013-10-10
      • 1970-01-01
      • 1970-01-01
      • 2019-12-05
      • 2019-07-20
      • 2011-08-02
      • 1970-01-01
      相关资源
      最近更新 更多