1. ## Need help please :) Newbie trying to learn

This is how I need the output of the program to look:

Welcome to My Mileage calculator

This program will calculate the miles per gallon for you for three tanks of gas after you have entered the gallons used and the 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 for this tank is 22.4

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

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

Your overall average miles per gallon for three tanks is 21.5

Thank you for using My mileage calculator program

This is what I have written so far. My question is how do I get it to calculate overall average miles per gallon for the all three tanks and also how do I get it to ask for tank 1, tank 2 , tank 3? Mine just says tank 1 on each one.

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

void main (void)
{
/*Variable Delclarations*/
/*----------------------*/
int   quotient, gallused, milesdriven;
float gallons_used;
float miles_driven;
float avg;

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

printf ("\nWelcome to Colleen's 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 #1: ");
scanf  ("%f", &gallons_used);
fflush (stdin);

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

gallons_used = miles_driven / gallons_used;

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

} // end of loop

gallons_used = avg / gallons_used;

printf ("\nYour overall average miles per gallon for three tanks is %.1f \n", avg);
fflush (stdin);

/*Display Results*/
/*---------------*/

printf ("\nThanks for using Colleen's Mileage calculator program!\n");

} //end main```

2. Code:
```printf ("\nEnter the number of gallons used for tank #1: ");
scanf  ("%f", &gallons_used);```
that is inside the for loop, so everytime the loop executes it asks the same thing, tank 1. you need 3 seperate printf prompts.
Code:
```printf ("\nEnter the number of gallons used for tank #1: ");
//calculation
printf ("\nEnter the number of gallons used for tank #2: ");
calculation
printf ("\nEnter the number of gallons used for tank #3: ");
calculation```
same thing for miles driven.

3. I am confused on where I would put the three printf statements to do that? sorry I am really new to programming and trying to teach myself.

Also how do I calculate the overalls miles per gallon for all three?

4. remove the for loop.

5. Just a couple of other pointers as well.

In Standard C, main is declared as returning an int:

You have:
Code:
`void main(void)`
and it should be:
Code:
`int main(void)`
Also, fflush(stdin) is an undefined behavior that can give wildly unpredictable results on different platforms. It should be avoided.

6. Originally Posted by paulntysmom
Also how do I calculate the overalls miles per gallon for all three?
first you need to find the total miles for all three and total gallons for all three, then average them. you are using avg the wrong way on your code, and avg is not defined.

7. THANKS FOR HELPING ME, I APPRECIATE IT VERY MUCH!

This is what I have so far, but still confused on calculating all three for the overall average. I can't figure out for the life of me on how to get that to work. I will keep trying though!

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

int main(void)
{
/*Variable Delclarations*/
/*----------------------*/
int   quotient, gallused, milesdriven;
float gallons_used;
float miles_driven;
float avg;

/*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*/
/*------------------*/

printf ("\nEnter the number of gallons used for tank #1: ");
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);

printf ("\nEnter the number of gallons used for tank #2: ");
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);

printf ("\nEnter the number of gallons used for tank #3: ");
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);

gallons_used = gallons_used / avg;

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

/*Output final greeting*/
/*---------------*/

printf ("\nThanks for using My Mileage calculator program!\n");

} //end main```

8. I just got your pm and I think I know what I am doing wrong now I will work on it and let you know

create an avg function and then you can just call it.

and you are using avg without it being initalized.

10. so...ummm....there was absolultly no problem with the for loop...it just had to be done differently in order to change the number each time.
Code:
`printf ("\nEnter the number of gallons used for tank #%d: ",gallused);`
And then the very last statement of the for loop should be something along these lines
Code:
`avg = avg+gallons_used;`
or
Code:
`avg += gallons_used;`
which is actually the same thing but thats another lesson.

Then, just before you print the overall average, you need to divide the avg by three.
Also, don't forget that you need to initiate avg as 0.0 at the beginning of the program.

11. Thank you crazedbrit I finally got it to work I appreciate everyones help

12. Also, fflush(stdin) is an undefined behavior that can give wildly unpredictable results on different platforms. It should be avoided.
See the FAQ: