# Can someone help me out with this program pleases?

• 02-26-2009
jturner38
Can someone help me out with this program pleases?
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; }```
• 02-26-2009
matsp
First of all it looks to me like you haven't initialized the tgallons and tmiles to zero.

The math here is also wrong:
Code:

```                tgallons = total + gallons;                 tmiles = total  + miles;```
You probably want to change total to the corresponding tX.

--
Mats
• 02-28-2009
jturner38
What do u mean by change the total to the corresponding tx? You mean like actually change total and replace it with a different variable?
• 02-28-2009
jturner38
Nm thanks I got it. Ill be sure to post more questions if i need help with my next program.