2011-09-21 36 views
1

我對此很新,所以請耐心等待。對於編碼C類,我們總共使用九個函數和一個頭文件(我們稱之爲my.h)來收集房間的長度和寬度(整數),百分比折扣(也是一個int )和地毯的單價(雙倍)。我們將使用輸入來計算安裝地毯的總成本。我可以得到打印的長度,寬度和麪積,但不是單價。它從Read_Data.c和Calc_Value.c函數打印frin,但不是在Calc_Value.c中調用的Install_Price.c中。 unit_price與長度和寬度同時讀取,但不知何故,它不能正確傳遞。我不知道爲什麼。也許它確實需要一個不同的指針。任何幫助將非常感激。我讀過我的書,並與我的教授和同學交談,同時也搜索了互聯網,但我沒有找到任何有用的東西。對於Install_Price的代碼是:打印在一個功能中的值工作正常,但打印在下一個功能中是錯誤的(返回0.00)

/*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; 
} 
+3

有**最少,完整**例子的問題通常會受到最多的關注。 –

+3

** TL; DR **〜〜〜 –

回答

2

您的問題是在Install_Price函數聲明中unit_price被聲明爲int,但在所有其他函數定義中聲明瞭double*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); 

解決該問題。

+0

+1實際上涉及這個問題... –

8

我沒看過整的問題,但是,我已經在這裏發現了一個錯誤:

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甚至不可忽略。

+1

+1甚至讀*它的一些*! –