在一个函数中打印的值工作正常,但在下一个函数中打印的值是错误的(返回 0.00)

发布于 2024-12-05 18:44:08 字数 5382 浏览 0 评论 0原文

我对此很陌生,所以请耐心等待。对于编码 C 类,我们总共使用九个函数和一个头文件(我们称之为 my.h)来收集房间的长度和宽度(整数)、折扣百分比(也是一个整数) ),以及地毯的单价(双)。我们将使用输入来计算安装地毯的总成本。我可以得到要打印的长度、宽度和面积,但不能得到单价。它从 Read_Data.c 和 Calc_Value.c 函数打印 frin,但不在 Install_Price.c 中打印,后者在 Calc_Value.c 中调用。 unit_price 与长度和宽度同时读取,但不知何故,它没有正确传递。我不知道为什么。也许它确实需要一个不同的指针。任何帮助将不胜感激。我读过我的书,与我的教授和同学交谈,并在互联网上搜索,但我没有找到任何有帮助的东西。 Install_Price 的代码是:

/*This function calculates the cost of the carpet and the labor cost in order to    
calculate the installed price.*/

#include "my.h"

void Install_Price (int length, int width, int unit_price, int* area, 
double* carpet_cost, double* labor_cost, double* installed_price)

{

printf("\nThe unit price is %7.2f.\n", *unit_price);

*area = length * width;
*carpet_cost = (*area) * unit_price;

printf("The carpet cost is %7d x %7.2f = %7.2f.\n", *area, unit_price, *carpet_cost);

*labor_cost = (*area) * LABOR_RATE;
*installed_price = (*carpet_cost) + (*labor_cost);

return;
}

请注意,这里的 printf 语句只是为了尝试找出我在 unit_price 上出错的地方。下面,我包含了 my.h 标头代码、main.c 以及它调用 Install_Price.c 的函数。再次感谢您的所有帮助!

my.h

#include <stdio.h>
void Read_Data(int* length, int* width, int* percent_discount, double* 
unit_price);

void Calc_Values(int length, int width, int percent_discount, double 
unit_price, int* area, double* carpet_cost, double* labor_cost, double* 
installed_price, double* discount, double* subtotal, double* tax, 
double* total);

void Install_Price(int length, int width, int unit_price, int* area, double*      
carpet_cost, double* labor_cost, 
double* installed_price);

void Subtotal (int percent_discount, double installed_price, double* discount, 
double* subtotal);

void Total (double subtotal, double* tax, double* total);

void Print (int length, int width, int area, double unit_price, double carpet_cost,    
double labor_cost, double 
installed_price, int percent_discount, double discount, double subtotal, double tax,   
double total);

void Print_Measurements (int length, int width, int area);

void Print_Charges (double unit_price, double carpet_cost, double labor_cost, double   
installed_price, int percent_discount, double discount, double subtotal, double tax, 
double total);

#define LABOR_RATE 0.35
#define TAX_RATE 0.085

main.c

/*  This function calls three subfuctions to calculate the costs of installing a    
carpet and prints an invoice.  */

#include "my.h"

int main (void)

{
        int length;
        int width;
        int percent_discount;
        double unit_price;
        int area;
        double carpet_cost;
        double labor_cost;
        double installed_price;
        double discount;
        double subtotal;
        double tax;
        double total;

Read_Data(&length, &width, &percent_discount, &unit_price);

Calc_Values(length, width, percent_discount, unit_price, &area, &carpet_cost,  
&labor_cost,&installed_price, &discount, &subtotal, &tax, &total);

Print(length, width, area, unit_price, carpet_cost, labor_cost,
installed_price, percent_discount, discount, subtotal, tax, total);

return 0;
}

Read_Data.c

/*This function asks the user for the length and width of a room to be carpeted, the  
percent discount and the unit price of the carpet. */

#include "my.h"

void Read_Data (int* length, int* width, int* percent_discount, double* unit_price)

{
printf("What is the length, in feet, of the room?\n");
scanf("%d", length);

printf("What is the width, in feet, of the room?\n"); 
scanf("%d", width);

printf("What is the percent discount?\n");
scanf("%d", percent_discount);

printf("What is the unit price of the carpet?\n");
scanf("%lf", unit_price);

printf("\nThe length is %6d.\n", *length);   //These printf statements work properly.
printf("The width is %6d.\n", *width);
printf("The percent discount is %3d%.\n", *percent_discount);
printf("The unit price is $%7.2f.\n", *unit_price);

return;
}

Calc_Value.c

/*This function calls three subfuctions that calculate all required quantities.  */

#include "my.h"

void Calc_Values (int length, int width, int percent_discount, double unit_price, 
int* area, double* carpet_cost, double* labor_cost, double* installed_price, double*  
discount, double* subtotal, double* tax, double* total)

{

printf("\nUnit Price:  %7.2f.\n", unit_price);  //This printf statement works properly.

Install_Price (length, width, unit_price, area, carpet_cost, labor_cost, 
installed_price);

Subtotal (percent_discount, *installed_price, discount, subtotal);

Total (*subtotal, tax, total);

return;
}

Install_Price.c(为了方便用户而重复)

/*This function calculates the cost of the carpet and the labor cost in order to    
calculate the installed price.*/

#include "my.h"

void Install_Price (int length, int width, int unit_price, int* area, 
double* carpet_cost, double* labor_cost, double* installed_price)

{

printf("\nThe unit price is %7.2f.\n", *unit_price);  //THIS DOES NOT WORK

*area = length * width;
*carpet_cost = (*area) * unit_price;

printf("The carpet cost is %7d x %7.2f = %7.2f.\n", *area, unit_price, 
*carpet_cost);  //THIS DOES NOT WORK

*labor_cost = (*area) * LABOR_RATE;
*installed_price = (*carpet_cost) + (*labor_cost);

return;
}

I'm very new to this, so please bear with me. For a coding C class, we're to use a total of nine functions and a header file (we're calling it my.h) to collect the length and width of a room (ints), the percent discount (also an int), and the unit price of the carpet (a double). We're to use the input to calculate the total cost to install the carpet. I can get the length, width and area to print, but not the unit price. It prints frin from the Read_Data.c and the Calc_Value.c functions, but not in Install_Price.c, which is called in Calc_Value.c. unit_price is read at the same time as length and width, but somehow, its not passed correctly. I'm not sure why. Maybe it does require a different pointer. Any help would be immensely appreciated. I've read my book and spoken to my professor and classmates, as well as searched the Internet, but I haven't found anything that helps. The code for Install_Price is:

/*This function calculates the cost of the carpet and the labor cost in order to    
calculate the installed price.*/

#include "my.h"

void Install_Price (int length, int width, int unit_price, int* area, 
double* carpet_cost, double* labor_cost, double* installed_price)

{

printf("\nThe unit price is %7.2f.\n", *unit_price);

*area = length * width;
*carpet_cost = (*area) * unit_price;

printf("The carpet cost is %7d x %7.2f = %7.2f.\n", *area, unit_price, *carpet_cost);

*labor_cost = (*area) * LABOR_RATE;
*installed_price = (*carpet_cost) + (*labor_cost);

return;
}

Please note, the printf statements here are just to try and figure out where I went wrong with unit_price. Below, I included the my.h header code, main.c, and the functions that it calls upto Install_Price.c. Again, thanks for all your help!

my.h

#include <stdio.h>
void Read_Data(int* length, int* width, int* percent_discount, double* 
unit_price);

void Calc_Values(int length, int width, int percent_discount, double 
unit_price, int* area, double* carpet_cost, double* labor_cost, double* 
installed_price, double* discount, double* subtotal, double* tax, 
double* total);

void Install_Price(int length, int width, int unit_price, int* area, double*      
carpet_cost, double* labor_cost, 
double* installed_price);

void Subtotal (int percent_discount, double installed_price, double* discount, 
double* subtotal);

void Total (double subtotal, double* tax, double* total);

void Print (int length, int width, int area, double unit_price, double carpet_cost,    
double labor_cost, double 
installed_price, int percent_discount, double discount, double subtotal, double tax,   
double total);

void Print_Measurements (int length, int width, int area);

void Print_Charges (double unit_price, double carpet_cost, double labor_cost, double   
installed_price, int percent_discount, double discount, double subtotal, double tax, 
double total);

#define LABOR_RATE 0.35
#define TAX_RATE 0.085

main.c

/*  This function calls three subfuctions to calculate the costs of installing a    
carpet and prints an invoice.  */

#include "my.h"

int main (void)

{
        int length;
        int width;
        int percent_discount;
        double unit_price;
        int area;
        double carpet_cost;
        double labor_cost;
        double installed_price;
        double discount;
        double subtotal;
        double tax;
        double total;

Read_Data(&length, &width, &percent_discount, &unit_price);

Calc_Values(length, width, percent_discount, unit_price, &area, &carpet_cost,  
&labor_cost,&installed_price, &discount, &subtotal, &tax, &total);

Print(length, width, area, unit_price, carpet_cost, labor_cost,
installed_price, percent_discount, discount, subtotal, tax, total);

return 0;
}

Read_Data.c

/*This function asks the user for the length and width of a room to be carpeted, the  
percent discount and the unit price of the carpet. */

#include "my.h"

void Read_Data (int* length, int* width, int* percent_discount, double* unit_price)

{
printf("What is the length, in feet, of the room?\n");
scanf("%d", length);

printf("What is the width, in feet, of the room?\n"); 
scanf("%d", width);

printf("What is the percent discount?\n");
scanf("%d", percent_discount);

printf("What is the unit price of the carpet?\n");
scanf("%lf", unit_price);

printf("\nThe length is %6d.\n", *length);   //These printf statements work properly.
printf("The width is %6d.\n", *width);
printf("The percent discount is %3d%.\n", *percent_discount);
printf("The unit price is $%7.2f.\n", *unit_price);

return;
}

Calc_Value.c

/*This function calls three subfuctions that calculate all required quantities.  */

#include "my.h"

void Calc_Values (int length, int width, int percent_discount, double unit_price, 
int* area, double* carpet_cost, double* labor_cost, double* installed_price, double*  
discount, double* subtotal, double* tax, double* total)

{

printf("\nUnit Price:  %7.2f.\n", unit_price);  //This printf statement works properly.

Install_Price (length, width, unit_price, area, carpet_cost, labor_cost, 
installed_price);

Subtotal (percent_discount, *installed_price, discount, subtotal);

Total (*subtotal, tax, total);

return;
}

Install_Price.c (repeating for user ease)

/*This function calculates the cost of the carpet and the labor cost in order to    
calculate the installed price.*/

#include "my.h"

void Install_Price (int length, int width, int unit_price, int* area, 
double* carpet_cost, double* labor_cost, double* installed_price)

{

printf("\nThe unit price is %7.2f.\n", *unit_price);  //THIS DOES NOT WORK

*area = length * width;
*carpet_cost = (*area) * unit_price;

printf("The carpet cost is %7d x %7.2f = %7.2f.\n", *area, unit_price, 
*carpet_cost);  //THIS DOES NOT WORK

*labor_cost = (*area) * LABOR_RATE;
*installed_price = (*carpet_cost) + (*labor_cost);

return;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白首有我共你 2024-12-12 18:44:08

我没有阅读整个问题,但是,我已经在这里发现了一个错误:

printf("\nThe unit price is %7.2f.\n", *unit_price);

并且

printf("The carpet cost is %7d x %7.2f = %7.2f.\n", *area, unit_price, *carpet_cost);

变量 unit_price 的类型为 int,但您将其打印为浮点数。

我猜第一个语句根本不应该编译,因为 unit_price 甚至不能取消引用。

I didn't read the entire question but, I've already spotted an error here:

printf("\nThe unit price is %7.2f.\n", *unit_price);

and

printf("The carpet cost is %7d x %7.2f = %7.2f.\n", *area, unit_price, *carpet_cost);

The variable unit_price is of type int, but you're printing it out as a floating-point.

I'm guessing the first statement shouldn't even compile at all since unit_price isn't even dereferencable.

很快妥协 2024-12-12 18:44:08

您的问题是,在 Install_Price 函数声明中,unit_price 被声明为 int,但在所有其他函数定义中,它被声明为 double*双。当您将该值传递给 Install_Price 时,C 会自动将 double 转换为 int。问题出在 Install_price 中的这两行代码:

printf("\nThe unit price is %7.2f.\n", *unit_price);

并且

printf("The carpet cost is %7d x %7.2f = %7.2f.\n", *area, 
       unit_price, *carpet_cost);

您正在使用 %f 打印单价,该单价已转换为 int.

Install_Price 的定义更改为

void Install_Price (int length, int width, double unit_price, int* area, 
double* carpet_cost, double* labor_cost, double* installed_price)

应该解决您的问题之一。

另一个问题也在这一行:

printf("\nThe unit price is %7.2f.\n", *unit_price);

unit_price 不应该在这一行中取消引用。将此更改为:

printf("\nThe unit price is %7.2f.\n", unit_price);

解决该问题。

Your issue is that in the Install_Price function declaration unit_price is declared an int, but in all other function definitions it is declared a double or *double. C is automatically casting the double to an int when you pass that value to Install_Price. The issue is with these two lines of code in Install_price:

printf("\nThe unit price is %7.2f.\n", *unit_price);

and

printf("The carpet cost is %7d x %7.2f = %7.2f.\n", *area, 
       unit_price, *carpet_cost);

You are using %f to print the unit price, which has been cast to an int.

Changing the definition of Install_Price to

void Install_Price (int length, int width, double unit_price, int* area, 
double* carpet_cost, double* labor_cost, double* installed_price)

Should solve one of your problems.

The other issue is also in this line:

printf("\nThe unit price is %7.2f.\n", *unit_price);

unit_price should not be dereferenced in this line. Change this to:

printf("\nThe unit price is %7.2f.\n", unit_price);

To fix that issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文