C初学者,代码输出不正确

Cel*_*ste 0 c printf scanf output

所以我对编码很新,这是我第一次使用scanf和printf.对于我的家庭作业,我们应该创建一个计算燃油效率的程序,单位为mpg,升数为每100km.最后的答案只应该有2个小数点......但这是另一个故事.,P

现在,该程序允许我输入第一部分的值(多少英里),但是,一旦我点击输入它跳过我的代码的末尾并吐出一个(看似)随机数?

#include <stdio.h> /* tells computer where to find definitions for printf and scanf */
#define KMS_PER_MILE 1.61 /* conversion constant for miles to kms */
#define LIT_PER_GAL 3.79 /* conversion constant for gallons to liters */

int main(void)
{
    double miles, /* input - distance in miles. */
    gallons, /* input - gallons consumed */
    mpg, /* output - miles per gallon */
    kms, /* output - kilometers */
    liters, /* output - liters */
    lpkm; /* output - liters per 100 kms */

    /* get the distance in miles */
    printf("Enter the distance in miles> ");
    scanf("%1f", &miles);

    /* get the gallons consumed */
    printf("Enter the gallons consumed> ");
    scanf("%1f", &gallons);

    /* convert to mpg */
    mpg = (double)miles / (double)gallons;

    /* convert to lpkm */
    liters = LIT_PER_GAL * gallons;
    kms = KMS_PER_MILE * miles;
    lpkm = (double)liters / (double)kms * 100;

    /* Display fuel efficiency in mpg and lpkm */
    printf("The car's fuel efficiency is\n %1f mpg \n %1f liters per 100 kms", mpg, lpkm);
    return (0);

}
Run Code Online (Sandbox Code Playgroud)

Vol*_*And 8

尝试改变%1f,以%lfscanf.

有关详细信息,请参阅C++参考