2016-02-26 737 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