Cant figure this one out. Ive attached it:)
Printable View
Cant figure this one out. Ive attached it:)
Read about the homework policy.
Can't figure this post out. I've quoted it. :)
My mind-reading crystal ball is in the shop for a little polishing, so please post your detailed question(s), about the program.
(I would add a getchar() after every scanf() to pull the newline char off the keyboard buffer.)
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;
}
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;
So %1.2f means "invalid input" in a scanf string. Presumably you mean %4f, which means "take at most four characters of input".
You might want to consider using floats or doubles in place of int in your calculations. Results from integer division are truncated.
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; }
when i replaced my floats with doubles (//Declare and //Output) it doesn't calculate and out puts $0.00
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.
I changed everything to doubles and now it works. Thank you everyone so very much.
You might also consider reading Scanf woes and Pause console.
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()*/
}