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

Eli*_*eth 1 c printf pointers function-pointers

我对此很新,所以请耐心等待.对于编码C类,我们总共使用九个函数和一个头文件(我们称之为my.h)来收集房间的长度和宽度(int),折扣百分比(也是一个int) ),和地毯的单价(双倍).我们将使用输入来计算安装地毯的总成本.我可以得到打印的长度,宽度和面积,但不是单价.它从Read_Data.c和Calc_Value.c函数打印frin,但不在Install_Calice.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;
}
Run Code Online (Sandbox Code Playgroud)

请注意,这里的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
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

Mys*_*ial 8

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

printf("\nThe unit price is %7.2f.\n", *unit_price);
Run Code Online (Sandbox Code Playgroud)

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

变量unit_price属于类型int,但您将其作为浮点打印出来.

我猜第一个语句甚至根本不应该编译,因为unit_price它甚至都不可用.