while ( gal != -1 ){
printf("\nEnter the gallons used ( -1 to end ): ");
scanf("%d", &gal);
printf("\nEnter the miles driven: ");
scanf("%d", &drive);
mg = drive/gal;
printf("\nThe miles / gallon for this tank was %d\n",mg);
cnt++;
}
if ( gal == -1 ) {
avg = mg/cnt;
printf("\nThe overall average miles/gallon for this tank was %d\n",avg);
}
You should change the order here. Try this:
Code:
do
{
printf("\nEnter the gallons used ( -1 to end ): ");
scanf("%d", &gal);
if( gal != -1 )
{
printf("\nEnter the miles driven: ");
scanf("%d", &drive);
mg = drive/gal;
printf("\nThe miles / gallon for this tank was %d\n",mg);
cnt++;
}
}
while ( gal != -1 );
avg = mg/cnt;
printf("\nThe overall average miles/gallon for this tank was %d\n",avg);
You may want to tweak your math a bit. (You may not need to, I'm tired and don't feel like compiling this or working it out in my head.) Anyway, that will fix your loop problem.
You could replace the do/while with a simple 'while' but you'd want to make sure and initialize 'gal' first before using it.
Quzah.