2011-09-21 10 views
1

저는 매우 새롭습니다. 코딩 C 클래스의 경우 총 9 개의 함수와 헤더 파일 (우리는 my.h라고 함)을 사용하여 방의 길이와 너비 (ints), 백분율 할인 (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는 doubleint으로 자동 전송합니다. 문제는 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); 

당신은 int로 캐스팅 된 단가를 인쇄 할 %f을 사용하고 있습니다.

문제 중 하나를 해결해야

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

Install_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_priceint 유형이지만 부동 소수점으로 인쇄합니다.

unit_price은 심지어 역 참조 할 수 없기 때문에 첫 번째 문장은 전혀 컴파일되지 않아야한다고 생각합니다.

+1

+1도 읽을 수 있습니다! –

관련 문제