Im trying to have all the miles added together and all the gallons added together then divide. For this code I have to have it add as many as I want until I input -1 in for gallons. I have this code correct for the most part its just that its not adding all the miles and all the gallons up. I have counter and counter1 counting how many times I've entered a mile and gallon.
Code:
#include <stdio.h>
int main ()
{
float miles;
float gallons;
int total;
float tmiles;
float tgallons;
float taverage;
float average;
int counter;
int counter1;
total = 0;
counter = 0;
counter1 = 0;
printf("Enter the gallons used (-1 to end): ");
scanf("%f", &gallons);
printf("Enter the miles driven:");
scanf("%f", &miles);
average = miles / gallons;
printf("The miles / gallons for this tank was %f\n\n", average);
while(gallons != -1) {
tgallons = total + gallons;
tmiles = total + miles;
printf("Enter the gallons used (-1 to end): ");
scanf("%f", &gallons);
if(gallons == -1)
break;
counter = counter + 1;
printf("Enter the miles driven:");
scanf("%f", &miles);
counter1 = counter1 + 1;
average = miles / gallons;
printf("The miles / gallons for this tank was %f\n\n", average);
counter++;
}
if(tgallons != 0) {
taverage = tmiles / tgallons;
printf("The overall average miles / gallons was %f\n", taverage);
}
return 0;
}