我无法让我的程序在计算区域后实际计算任何值。
它计算得很好,我为自己获得无bug而自豪。
但现在它只是吐出空白行,并在我的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
答案 0 :(得分:2)
如评论中所述,您的主要问题是在scanf
中使用了错误的格式说明符。 double
的正确格式说明符为%lf
,而非%f
。使用错误的格式说明符调用未定义的行为。
所以改变
scanf("%f", &costPersqft);
到
scanf("%lf", &costPersqft);
您还应该检查所有scanf
的返回值。在您的情况下,如果成功,所有这些都将返回1.
如果您启用了编译器,编译器会向您显示警告。使用-Wall -Wextra
启用警告。注意警告,不要忽视它们。
此外,Nested functions are a GCC extenion为said by @MikeCAT在您提问的评论中。它不是标准C.所以我建议移动
int get_Area (int x, int y)
{
return(x * y);
}
就在main
的定义之前。
答案 1 :(得分:0)
它在底部混乱,但这些结果直接来自我的程序。
#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平方英尺
收费
说明COST / SQ.FT。收费
----------- ----------- -----------
地毯成本22.25 $ 3426.50
工党0.35 53.90
-----------
安装价格$ 3480.40
折扣0.10 348.04
-----------
小计$ 3132.36
税266.25
总计$ 3398.61