2012-10-22 75 views
0
#include <stdio.h> 
#include <stdlib.h> 
#include <math.h> 
#define CONST 267 


void getInput(int *length, int *width, int *height); 
void calcoutput(int length, int width, int height, int *squareFootage,int   *paintNeeded); 
int getSquareFootage(int length,int width, int height); 
double getPaintNeeded(int squareFootage); 


int main(void) 
{ 
    int length; 
    int width; 
    int height; 
    int squareFootage; 
    double paintNeeded; 


    getInput(&length, &width, &height); 
    calcoutput(length, width, height,&squareFootage,&paintNeeded); 


    return 0; 
} //end main 

void getInput(int *plength, int *pwidth, int *pheight) 
{ 
    printf("Enter the length of the room (whole number only): "); 
    scanf("%d", plength); 
    printf("Enter the width of the room (whole number only): "); 
    scanf("%d", pwidth); 
    printf("Enter the height of the room (whole number only): "); 
    scanf("%d", pheight); 
} //end getInput 
void calcoutput(int length, int width, int height, int *squareFootage,int *paintNeeded){ 

    *squareFootage = getSquareFootage(length,width, height); 
    *paintNeeded = getPaintNeeded(squareFootage); 

} 

int getSquareFootage(int length,int width, int height){ 
    int i; 
    i = 2*(length* height) + 2*(width*height) + (length* width); 
return i; 
} 
double getPaintNeeded(int squareFootage) 
{ 
    double i = double (squareFootage/CONST); 
    return i; 
} 

我寫這篇文章的代碼來計算粉刷房間需要油漆加侖的房間和數量的區域時警告信息,但是,我不是很熟悉C指針,似乎有一些錯誤和警告這樣錯誤和使用指針

C:\Users\khoavo\Desktop\hw2b.c||In function 'main':| 
C:\Users\khoavo\Desktop\hw2b.c|23|warning: passing argument 5 of 'calcoutput' from incompatible pointer type| 
C:\Users\khoavo\Desktop\hw2b.c|8|note: expected 'int *' but argument is of type 'double *'| 
C:\Users\khoavo\Desktop\hw2b.c||In function 'calcoutput':| 
C:\Users\khoavo\Desktop\hw2b.c|41|warning: passing argument 1 of 'getPaintNeeded' makes integer from pointer without a cast| 
C:\Users\khoavo\Desktop\hw2b.c|10|note: expected 'int' but argument is of type 'int *'| 
C:\Users\khoavo\Desktop\hw2b.c||In function 'getPaintNeeded':| 
C:\Users\khoavo\Desktop\hw2b.c|52|error: expected expression before 'double'| 
||=== Build finished: 1 errors, 2 warnings ===| 

我如何能夠解決這些錯誤和警告? 預先感謝您。

回答

0

變化

void calcoutput(int length, int width, int height, int *squareFootage,int *paintNeeded); 

void calcoutput(int length, int width, int height, int *squareFootage,double *paintNeeded); 
0

paintNeeded被宣佈爲雙,但你通過它的位置作爲一個指針爲int。你傳遞給它的函數會看到整數值而不是雙精度值,這會讓你的程序運行不正確。

您應該考慮將calcoutput中的int *轉換爲double *,以便在paintNeeded中傳遞將表現正確。

1

錯誤消息說明了一切:

calcoutput接受一個int *作爲其第五個參數,但你傳遞一個雙*。將第五個參數更改爲double *。

getPaintNeeded需要一個整數,但你傳遞給它一個int *。我認爲你在這種情況下想要的是getPaintNeeded(*squareFootage)

最後一個錯誤是關於演員。您正在使用的支持在C函數樣式轉換++而不是在C,而你作爲編譯C.無論是作爲編譯C++(改變文件擴展名.CPP),或將該行更改爲:

double i = (double)(squareFootage/CONST); 

其實你根本不需要演員,結果可以隱式轉換爲雙精度型。

+0

應該在分割之前完成'(double)squareFootage/CONST',否則你會得到整數除法,這可能不是OP想要的。 –