C语言中使用的未初始化的局部变量

时间:2016-02-26 22:38:32

标签: c

所以我一直在这一点,我有点难过,我得到“未初始化的局部变量加仑使用”。在我的take_Input函数转换(加仑);

我知道这意味着加仑的价值无法被识别。

为什么加仑到升的功能不会为加仑设置值,以便转换函数具有该值。任何帮助表示感谢...

代码:

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);
}

1 个答案:

答案 0 :(得分: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并删除{{1这些函数的语句。