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

标签 c pointers printf function-pointers

我对此很陌生,所以请多多包涵。对于编码 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 的函数。再次感谢您的帮助!

我的.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

主.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;
}

读取数据.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;
}

计算值.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;
}

最佳答案

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

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_priceint 类型,但您将其打印为 float 。

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

关于c - 在一个函数中打印的值工作正常,但在下一个函数中打印的值是错误的(返回 0.00),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7493801/

相关文章:

c++ - 语法错误 : Call Member Function Pointer using Pointer to Object

c - 尝试访问结构指针的第一个成员(本身是一个 volatile 指针)不会返回存储的成员指针,而是返回结构地址

c - snprintf 中的 %c%.8x 格式意味着什么

c++ - C++ 中的 fprintf 和 vfprintf 有什么区别?

linux - 如何使用汇编语言从键盘读取输入字符串

c - stdarg.h 在哪里?

c++ - OpenCV:如何绘制一条线,其颜色相对于应绘制的表面相反?

c - 如何将二维数组传递给C中的函数?

c - libcurl 出现编译错误

c++ - C++ 禁止指针和整数的比较