2012-10-21 3 views
-1
#include <stdio.h> 

#define GA_OF_PA_NEED 267.0 

int getSquareFootage(int squareFootage); 
double calcestpaint(int squareFootage); 
double printEstPaint(double gallonsOfPaint); 

int main(void) 

{ 
    //Declaration 
    int squareFootage = 0; 
    double gallonsOfPaint = 0; 


    //Statements 
    getSquareFootage(squareFootage); 
    gallonsOfPaint = calcestpaint(squareFootage); 
    gallonsOfPaint = printEstPaint(gallonsOfPaint); 
    system("PAUSE"); 
    return 0; 
} 

int getSquareFootage(int squareFootage) 
{ 
    printf("Enter the square footage of the surface: "); 
    scanf("%d", &squareFootage); 
    return squareFootage; 
} 

double calcestpaint(int squareFootage) 
{ 
    return (double) (squareFootage * GA_OF_PA_NEED);  
} 
double printEstPaint(double gallonsOfPaint) 
{ 

    printf("The estimate paint is: %lf\n",gallonsOfPaint); 
    return gallonsOfPaint; 
} 

출력이 gallonsOfPaint를 0.0으로 표시하는 이유는 무엇입니까? 오류가없고 모든 것이 논리적으로 올바른 것 같습니다. calc 함수에서 calculate 문에 문제가있는 것 같습니다.출력이 잘못된 결과를 반환합니다.

+3

흠, 왜냐하면 :'0 * 267 == 0' 당신은 어떤 가치를 기대 했습니까? – amit

답변

2

당신은 getSquareFootage(squareFootage);의 결과에 할당해야합니다 squareFootage으로

squareFootage = getSquareFootage(squareFootage); 

는 값이 아닌 참조 또는 다른 말로 전달을, 당신은 함수에서 변경 얼마나 중요하지 않습니다 함수 밖에서는 아무 효과가 없습니다. 양자 택일로, 당신은 참조로 전달할 수 없었다 : 다음과 같이 호출됩니다

void getSquareFootage(int * squareFootage) 
{ 
    printf("Enter the square footage of the surface: "); 
    scanf("%d", squareFootage); 
} 

:

getSquareFootage(&squareFootage); 
+0

@Downvoter - 의견을 주셔서 감사하겠습니다. – MByD

1

수정을이 squareFootage=getSquareFootage();

로 매개 변수를 전달하기 위해 필요합니다.

1

squareFootage 변수를 업데이트하지 않습니다. calcestpaint (squareFootage)를 호출하면 값 0을 인수로 전달합니다.

관련 문제