-5

这里有没有 C 语言程序员可以帮助我解决这个问题?我在计算每加仑平均英里数时遇到了麻烦,我头晕目眩。如果有人有解决方案,我将不胜感激^_^

int x, number_of_tanks = 3;
double total_num1, total_num2;
double total_miles_per_gallon;
float division, avg;
float num1, num2;

for (x = 1; x <= 3; x++)
{
    printf("Enter the number of gallons used for tank #%i: ",x);
    scanf("%f", &num1);
    fflush(stdin);      /* clear input buffer */

    printf("Enter the number of miles driven: ");
    scanf("%f", &num2);
    fflush(stdin);      /* clear input buffer */

    /*--------------------------------------------------------------*/
    /* calculate and output the miles per gallon from user input.   */
    /* ------------------------------------------------------------ */

    division = num2 / (float) num1;                            
    printf("The miles per gallon for this tank %.1f divided by %.1f is %.1f", \
        num2, num1, division);

    total_num2 = total_num2 + num2;
    printf("The total of miles is %f\n", total_num2);

    total_num1 = total_num1 + num1;
    printf("The total of gallons is %f\n", total_num1);
}

avg = (double) total_num2 / total_num1; 
printf("Overall average miles per gallon for three tanks: %.1f", avg);
4

1 回答 1

1

你没有初始化你的总数,所以它们是未定义的。当您开始添加它们时,您会得到未定义的结果。我敢打赌,这就是您所说的它不起作用的意思。

做这个:

double total_num1 = 0;
double total_num2 = 0;
于 2013-06-28T02:55:22.827 回答