1. ## Gas prices, stumped!

Cant figure this one out. Ive attached it

3. Originally Posted by ahealy88
Cant figure this one out. Ive attached it
Can't figure this post out. I've quoted it.

(I would add a getchar() after every scanf() to pull the newline char off the keyboard buffer.)

4. Consider posting the code on the board with code tags as well. People who don't use Windows will have less trouble with newlines / carriage returns and so on. I will post it here, but there are no changes to it.

Code:
```/* Alison Healy
COP 3223
Section 2
Assignment 1 - Problem A: Gas Expenses
1/26/2009 */

#include <stdio.h>
int main(void)
{
//Variables
int mi_per_gal, size_tank_gal, mi_per_month;
float price_per_gal, cost_per_month;
printf("How many miles per gallon does your vehicle get?\n");
scanf("%d", &mi_per_gal);
printf("What is the size of your gastank in gallons?\n");
scanf("%d", &size_tank_gal);
printf("What is the price of gas per gallon?\n");
scanf("%1.2f", &price_per_gal);
printf("How many miles do you drive in a month?\n");
scanf("%d", &mi_per_month);
printf("\n");
cost_per_month = (mi_per_month / mi_per_gal) * price_per_gal;

//Output
printf("The cost of gas for the month is \$%.2f.\n", cost_per_month);
system("pause");
return 0;
}```

5. I can enter input for the first 3 questions, but the last question doesnt allow me to enter anything, I think it goes wrong right around here:

printf("How many miles do you drive in a month?\n");
scanf("%d", &mi_per_month);
printf("\n");
cost_per_month = (mi_per_month / mi_per_gal) * price_per_gal;

6. So %1.2f means "invalid input" in a scanf string. Presumably you mean %4f, which means "take at most four characters of input".

7. You might want to consider using floats or doubles in place of int in your calculations. Results from integer division are truncated.

8. It works now, but I when I run a test with:
24 mpg
12 gallons in a tank
3.00 per gallon
1000 miles a month
I should get \$125, but I get \$123
I've been working on this for days
Code:
```#include <stdio.h>

int main(void)        {
//Variables
int mi_per_gal,size_tank_gal,mi_per_month;
float price_per_gal,cost_per_month;

printf("How many miles per gallon does your vehicle get?\n");
scanf("%d", &mi_per_gal);

printf("What is the size of your gastank in gallons?\n");
scanf("%d", &size_tank_gal);

printf("What is the price of gas per gallon?\n");
scanf("%4f", &price_per_gal);

printf("How many miles do you drive in a month?\n");
scanf("%d", &mi_per_month);

//Calculate
cost_per_month=(mi_per_month/mi_per_gal)*price_per_gal;

//Output
printf("The cost of gas for the month is \$%.2f.\n",cost_per_month);

system("pause");

return 0;       }```

9. when i replaced my floats with doubles (//Declare and //Output) it doesn't calculate and out puts \$0.00

10. 1000/24 = 41, not 41.6666667. If you intend to divide integers and get a decimal answer, well, you won't. You should probably rearrange the calculation to multiply by the float first, then divide.

11. Originally Posted by ahealy88
when i replaced my floats with doubles (//Declare and //Output) it doesn't calculate and out puts \$0.00
You need to change other things to be consistent -- for instance scanf("%lf") for doubles -- scanf("%f") won't work.

12. I changed everything to doubles and now it works. Thank you everyone so very much.

13. You might also consider reading Scanf woes and Pause console.

14. ## Similar

Since you have solved it, can I just say it looks very similar to this, which is what I did back in 2001:

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

/*main function - driver
**/
int main ( int argc, char *argv[] ) {
int milesDriven = 0;
float gallonsUsed = 0,
totalMPG = 0;

puts("Enter the gallons used ( enter -1 to quit):" );
scanf("%f", &gallonsUsed);

while ( gallonsUsed != -1 ) {
puts("Enter the miles driven:");
scanf("%d", &milesDriven);

/*calculate total milage*/
totalMPG = milesDriven / gallonsUsed;

printf("Total MPG for this tankful: %.6f\n", totalMPG);

puts("\nEnter the gallons used ( enter -1 to quit):");
scanf("%f", &gallonsUsed);
}

getchar(); /*freeze console output*/

return 0; /*return value from int main()*/
}```

15. Originally Posted by swgh
Since you have solved it, can I just say it looks very similar to this, which is what I did back in 2001:
which contains several minor problems like using != for float var
not checking return value of scanf
not validating user input
not clearing input buffer from garbage if scanf failed