# miles per gallon program

• 09-27-2002
JamesCole
miles per gallon program
This program is just supposed to ask the user 3 questions and get a calculation of the miles per gallon over and over until the user types a negative number. I have 2 problems currently, first the miles per gallon comes out to be some odd number (ex. 15000 initial miles, 15250 final miles, 10 gallons, should be 25 miles per gallon but it comes out to be 1254792).

Second its supposed to ask all 3 questions again after it calculates the miles per gallon...but instead it just start off asking the 2nd question after it gives the weird calculation. Any idea what I'm doing wrong?

Code:

#include <stdio.h>

int main()
{
int  input_value, final, gallons, milespergallon;

scanf("%d", &input_value);

while ( input_value > 0 )
{
scanf("%d", &final);

printf("What is the number of gallons of gas:\n");
scanf("%d", &gallons);
milespergallon = ( final - input_value ) / gallons;

printf("Miles per gallon: %d, &milespergallon");
}
return 0;
}

-James

• 09-27-2002
Cshot
The fault was in this line:
printf("Miles per gallon: %d, &milespergallon");
Also your quit logic was a little off.

This should do the job.
Code:

#include <stdio.h>

int main()
{
int input_value, final, gallons, milespergallon;

scanf("%d", &input_value);

while ( input_value > 0 )
{
scanf("%d", &final);

printf("What is the number of gallons of gas:\n");
scanf("%d", &gallons);
milespergallon = ( final - input_value ) / gallons;

printf("Miles per gallon: %d", milespergallon);

scanf("%d", &input_value);
}
return 0;
}

EDIT -
Quote:

Beat by Lightening boy :D
That's cuz you're too busy editting code tags. You and your superpowers ;)
• 09-27-2002
Hammer
>>printf("Miles per gallon: %d, &milespergallon");
This is wrong. Try this instead:

>>printf("Miles per gallon: %d\n", milespergallon);

Beat by Lightening boy :D
• 09-27-2002
JamesCole
thanks, it worked perfectly
• 10-02-2002
VegasSte
I probably would have done it like this!
I personally would have done it like this.
Code:

#include <stdio.h>

int main()
{
int input_value, final, gallons, milespergallon;

do{
scanf("%d", &input_value);