# Can't figure out for the life of me what I am doing wrong.

This is a discussion on Can't figure out for the life of me what I am doing wrong. within the C Programming forums, part of the General Programming Boards category; I have been working on this one program for 3 weeks now and can't figure out for the life of ...

1. ## Can't figure out for the life of me what I am doing wrong.

I have been working on this one program for 3 weeks now and can't figure out for the life of me what I am doing wrong.

I can't calculate the overall average for (total miles divided by total gallons)

This is what my output is suppose to look like:

Welcome to my mileage calculator.

This program will calculate the miles per gallon for ou for three tanks of gas after you have entered the gallons used and miles driven.

Enter the number of gallons used for tank #1: 12.8
Enter the number of miles driven: 287.1
***The miles per gallon used for this tank is 22.4

Enter the number of gallons used for tank #1: 10.3
Enter the number of miles driven: 200.2
***The miles per gallon used for this tank is 19.4

Enter the number of gallons used for tank #1: 5.2
Enter the number of miles driven: 120.9
***The miles per gallon used for this tank is 23.3

Your overall average miles per gallon for three tanks is 21.5

Thanks for using my mileage calculator program!

This is what I have written so far! Please help me! What am I doing wrong? My overall average is coming up to 26.2?????
Code:
```#include <stdio.h>

int main ()
{
/*Variable Declarations*/
/*----------------------*/

int   quotient, gallused;
float gallons_used;
float miles_driven;
float total_miles;
float total_gal=0;

/*Display program info*/
/*--------------------*/

printf ("\nWelcome to my mileage calculator.\n");
printf ("\nThis program will calculate the miles per gallon for you for\n");
printf ("three tanks of gas after you have entered the gallons used and\n");
printf ("miles driven.\n");

/*Prompt user for information*/
/*---------------------------*/

for (gallused = 1; gallused <= 3; ++ gallused )
{
printf ("\nEnter the number of gallons used for tank #%d: ",gallused);
scanf  ("%f", &gallons_used);

printf ("Enter the number of miles driven: ");
scanf  ("%f", &miles_driven);

gallons_used = miles_driven / gallons_used;

printf ("***The miles per gallon used for this tank is %.1f \n",gallons_used);

total_gal = gallons_used + total_gal;
total_miles = total_miles + miles_driven;

} // end for loop

printf ("\nYour overall average miles per gallon for three tanks is %.1f \n",total_miles / gallons_used);

/*Display End Comment*/
/*---------------*/

printf ("\nThanks for using my mileage calculator program!\n");

} //end main```

2. set total_miles to 0 at the begin and your problem's solved

3. Thanks for the help I set the total_miles to 0 but I am still coming up with 26.2 instead of the required 21.5??? Any idea's why?

4. Sorry, spoke out of line

You want to save gallons_used rather then total_gal and total_miles

The average at the end will be gallons_used/3.0

5. ok I am confused??? Sorry I am a newbie trying to teach myself.

You want to save gallons_used rather then total_gal and total_miles

The average at the end will be gallons_used/3.0

So I would replace this:

Code:
`printf ("\nYour overall average miles per gallon for three tanks is %.1f \n",total_miles / gallons_used);`
with this:
Code:
`printf ("\nYour overall average miles per gallon for three tanks is %.1f \n",gallons_used/3.0);`
Also do I eliminate this all together
Code:
```		total_gal = gallons_used + total_gal;
total_miles = total_miles + miles_driven;```
I tried doing the above but now I am coming up with 7.8 so I know I am still doing something wrong

6. Code:
```#include <stdio.h>

int main ()
{
/*Variable Declarations*/
/*----------------------*/

int   quotient, gallused;
float miles_gallons_used,gallons_used;
float miles_driven;
float total_miles=0;
float total_gal=0;

/*Display program info*/
/*--------------------*/

printf ("\nWelcome to my mileage calculator.\n");
printf ("\nThis program will calculate the miles per gallon for you for\n");
printf ("three tanks of gas after you have entered the gallons used and\n");
printf ("miles driven.\n");

/*Prompt user for information*/
/*---------------------------*/

for (gallused = 1; gallused <= 3; ++ gallused )
{
printf ("\nEnter the number of gallons used for tank #%d: ",gallused);
scanf  ("%f", &gallons_used);

printf ("Enter the number of miles driven: ");
scanf  ("%f", &miles_driven);

miles_gallons_used = miles_driven / gallons_used;

printf ("***The miles per gallon used for this tank is %.1f \n",miles_gallons_used);

total_gal = gallons_used + total_gal;
total_miles = total_miles + miles_driven;

} // end for loop

printf ("\nYour overall average miles per gallon for three tanks is %.1f \n",total_miles /

total_gal);

/*Display End Comment*/
/*---------------*/

printf ("\nThanks for using my mileage calculator program!\n");

} //end main```

that works and u get 21.5,so check out what u were doing wrong.

7. Thank you so much I now understand what I was missing. I am happy to know I was almost there. It's all still a learning process for me. I am a newbie, but hope to someday become a programmer. I love this board!

8. Originally Posted by paulntysmom
Thank you so much I now understand what I was missing. I am happy to know I was almost there. It's all still a learning process for me. I am a newbie, but hope to someday become a programmer. I love this board!
Hang in there . You're doing the right stuff. You're attacking your problem spots, you're asking for help, and you want to learn. In my personal opinion, those are three key ingredients to becoming a programmer...

(And of course, you just have to love this stuff too )