Originally Posted by
whiteflags
Is there a reason you decided to paste on image instead of code? From a glance nothing looks suspicious except for
fflush(stdin); - how exactly is the file wrong?
Yeah, I'm not sure why I didnt do that. I don't know whats off. Im getting a value of .1 for the avg output after the loop
Code:
<stdio.h>
int main (void)
{
/*Variable Delclarations*/
/*----------------------*/
int trip;
float mpg;
float miles_driven = 0.0;
float avg = 0.0;
float gallons;
float total_gallons = 0.0;
float miles;
/*Display program info*/
/*--------------------*/
printf ("Welcome to the mileage calculator.\n\n");
printf ("This program will calculate the miles per gallon for you for three tanks of gas after you have entered the gallons used and miles driven.\n\n");
/*User Input*/
/*----------*/
for (trip = 1; trip <= 3; ++ trip)
{
printf ("\nEnter the number of gallons used for tank #%d:", trip);
scanf ("%f", &gallons);
fflush (stdin);
total_gallons = gallons + total_gallons;
printf ("Enter the number of miles driven: ");
scanf ("%f", &miles);
fflush (stdin);
mpg = miles / gallons;
miles_driven = miles + miles_driven;
avg = total_gallons / miles_driven;
printf ("***The miles per gallon used for this tank is %.1f \n",mpg);
fflush (stdin);
} // end of loop
/*Display Results*/
/*---------------*/
printf ("\nYour overall average miles per gallon for three tanks is %.1f \n", avg);
fflush (stdin);
printf ("\nThanks for using the Mileage calculator program!\n");
return 0;
} /* End Main */