2016-02-26 91 views
1

所以我一直在這一點,我有點難倒我得到「未初始化的局部變量加侖使用」。在我的take_Input函數轉換(加侖);在C語言中使用未初始化的局部變量

我知道這意味着加侖的價值不被認可。

爲什麼加侖升功能沒有爲加侖設置一個值,以便轉換函數具有該值。任何幫助讚賞感謝...

代碼:

double take_Input(void) 
{ 
    double a, b, c, d; 
    double gallons; 

    printf("please enter how many liters of A: "); 
    scanf("%lf", &a); 
    printf("please enter how many gallons of B: "); 
    scanf("%lf", &b); 
    printf("please enter how many liters of C: "); 
    scanf("%lf", &c); 
    printf("please enter how many gallons of D: "); 
    scanf("%lf", &d); 

    gallons_To_Liters(a,b,c,d); 

    conversions(gallons); 
    return(0); 
} 

double gallons_To_Liters(double a, double b, double c,double d) 
{ 
    double gallons, liters; 

    liters = a + c; 
    gallons = b + d; 
    gallons = (liters * 3.79) + gallons; 
    return(0); 
} 

double conversions(double gallons) 
{ 
    double totalGallons = gallons; 
    double quarts = totalGallons * 4; 
    double pints = totalGallons * 8; 
    double cups = totalGallons * 16; 
    double fluid_ounces = totalGallons * 128; 
    double tablespoons = totalGallons * 256; 
    double teaspoons = totalGallons * 768; 
    // output statements. 
    printf("the amount of gallons is: %.2f \n", totalGallons); 
    printf("the amount of quarts is: %.2f \n", quarts); 
    printf("the amount of pints is: %.2f \n", pints); 
    printf("the amount of cups is: %.2f \n", cups); 
    printf("the amount of fluid ounces is: %.2f \n", fluid_ounces); 
    printf("the amount of tablespoons is: %.2f \n", tablespoons); 
    printf("the amount of teaspoons is: %.2f \n", teaspoons); 
    return (0); 
} 
+0

也許你應該熟悉* scope *的概念。 – EOF

+0

@EOF也許當地是美國加侖.. –

+0

當你有2加侖的應用程序時,誰需要10加侖的帽子? –

回答

1

gallons_To_Liters功能設置本地變量gallons,但確實與它無關。你需要從函數中返回這個值。

然後在調用函數中,您需要將返回值gallons_To_Liters分配給該函數中的gallons變量。

double take_Input(void) 
{ 
    .... 
    gallons = gallons_To_Liters(a,b,c,d); 
    .... 
} 

double gallons_To_Liters(double a, double b, double c,double d) 
{ 
    double gallons, liters; 

    liters = a + c; 
    gallons = b + d; 
    gallons = (liters * 3.79) + gallons; 
    return gallons; 
} 

您需要記住,不同功能的變量彼此不同,即使它們的名稱相同。

此外,對於take_Inputconversion功能,其返回值不用於任何東西,所以更改功能有void返回類型並刪除這些功能的return語句。

+0

感謝幫助。這工作,我只在我的第二個真正的學期編程和採取Java和C在同一時間,所以作爲一個初學者,我一定會遇到一些問題。 – jjflyV7