1. ## consumption

i wrote this program, but somthenig is missing when i input -1 to finish. instead to have straight this :the overall average miles/gallons:.... my average consumption result, the program output this: Enter the miles driven and it follows:the overall average miles/gallons:

itwil be kind, i had two days stugling.
Code:
```#include <stdio.h>
#include <stdlib.h>

int main()
{
int miles;
int gallons;
int counter;
int total;

float average;
float consumption;

counter = 0;
total =  0;

printf ("Enter the gallons used, -1 to end :");
scanf("%d", &gallons);
printf("Enter the miles driven :");
scanf("%d", &miles);

while (gallons != -1){
consumption = (float) miles / gallons;
printf("The miles / gallons for this thank was %.2f \n", consumption);

total += consumption;
counter += 1;

printf ("\nEnter the gallons used, -1 to end :");
scanf("%d", &gallons);

printf("Enter the miles driven :");
scanf("%d", &miles);
}

if (counter != 0){
average = (float) total / counter;
printf ("\nthe overall average miles/gallons %.2f \n", average);
}

else{
printf ("\nNo  No No  ");
}

system("PAUSE");
return 0;
}```

2. please indent your code better next time. Here's the new indented code:
Code:
```#include <stdio.h>
#include <stdlib.h>

int main()
{
int miles;
int gallons;
int counter;
int total;

float average;
float consumption;

counter = 0;
total =  0;

printf ("Enter the gallons used, -1 to end :");
scanf("&#37;d", &gallons);
printf("Enter the miles driven :");
scanf("%d", &miles);

while (gallons != -1){
consumption = (float) miles / gallons;
printf("The miles / gallons for this thank was %.2f \n", consumption);

total += consumption;
counter += 1;

printf ("\nEnter the gallons used, -1 to end :");
scanf("%d", &gallons);

printf("Enter the miles driven :");
scanf("%d", &miles);
}

if (counter != 0){
average = (float) total / counter;
printf ("\nthe overall average miles/gallons %.2f \n", average);
}

else{
printf ("\nNo  No No  ");
}

system("PAUSE");
return 0;
}```

3. Compare the differences from the changes I have made.

1) You were taking in a -1 for gallons, but then acting upon it as if it were a valid value in the loop. Having a WHILE condition at the top of the loop was not correct.

2) Now, the questions are only asked in one place, instead of two places.

3) The overall calculation is now correct. You can't save the "consumption" (which was the average miles per gallon) and just add to it time and time again.

Todd

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

int main(void)
{
int miles, total_miles = 0 ;
int gallons, total_gallons = 0 ;
int counter = 0 ;

float average;
float consumption;

while (1) {

printf ("Enter the gallons used, -1 to end :");
scanf("&#37;d", &gallons);
if (gallons <= 0) break ;

total_gallons += gallons ;

printf("Enter the miles driven :");
scanf("%d", &miles);

if (miles <=0) break ;

total_miles += miles ;

consumption = (float) miles / gallons;
printf("The miles / gallons for this thank was %.2f \n", consumption);
counter += 1;
}

if (counter != 0){
average = (float) total_miles / total_gallons ;
printf ("\nthe overall average miles/gallons %.2f \n", average);
}

system("PAUSE");
return 0;
}```

4. you'll need to put a break statement in the loop after -1 has been input.

EDIT: sorry, Todd beat me.

5. Originally Posted by Abda92
you'll need to put a break statement in the loop after -1 has been input.

EDIT: sorry, Todd beat me.
thanks very much, it did really get the result i have been waiting. the thing, is i don't want to use the worb "break" in the program, because i can't show it to the teacher, we stil didn t get to this level of programming., I need it as it is but with adjustment.

6. Then just put...
Code:
`while (miles <= 0)`
`system("PAUSE");`