"yanyo" <x_******@hotmail.com> writes:
hi, im starting out in C prog and im stuck in this code, its supposed to
calculate this inputs and give out the total expense of gas...
#include <stdio.h>
int main() {
int miles_per_gallon, gastank, miles_per_month;
float total_cost, gas_price;
total_cost= gastank*gas_price/miles_per_gallon*miles_per_month/12;
printf("How many miles per gallon does your car get?\n");
scanf("%d", &miles_per_gallon);
printf("What is the size of your gas tank in gallons?\n");
scanf("%f", &gastank);
printf("What is the price of gasoline per gallon?\n");
scanf("%d", &gas_price);
printf("How many miles do you drive in a month?\n");
scanf("%d", &miles_per_month);
printf("The cost of gas for the month is %1.2f\n",total_cost);
return 0;
}
for some reason i only get zero when i run it i tried changing parethesis
but that didnt work either..
You have to remember that the statements are executed in order, and an
assignment is not a definition. For example, an assignment statement
like:
x = y + z;
stores the sum of y and z *at the point the statement is executed* in x.
Changing y or z later doesn't affect the value of x unless you assign
a new value to x. (In mathematics, on the other hand, something like
"x = y + z" is more likely to be a definition.)
After the declarations of miles_per_gallon, gastank, miles_per_month,
total_cost, and gas_price, all those variables have garbage values.
The values may not even be valid numbers. The value you then assign
to total_cost is computed from garbage, yielding garbage; it looks
like you happen to get a result of 0.0, but it could be anything.
You need to compute the value of total_cost *after* you've set the
values on which it depends.
That's the big problem. There are some minor ones as well.
The declaration "int main()" is acceptable, but "int main(void)" is
better and more explicit.
Indentation is important, especially as programs become more complex.
Everything between the opening and closing braces should be indented,
probably by 4 spaces.
You declare miles_per_gallon, gastank, and miles_per_month as int.
Realistically, these could be real numbers; for example, a gas tank
might hold 14.5 gallons.
It doesn't matter for a toy program like this, but generally there's
not much benefit in using float rather than double. (This means you
need to use "%lf" rather than "%f" to read the values.)
You use a "%f" format to read the value for gastank, which you declared
as an int, and a "%d" format to read the value for gas_price, which
you declared as a float.
The literal 12 in the assignment statement should be 12.0, a real
number. As it happens, it will be promoted to floating-point anyway,
but it's best to be explicit.
It doesn't matter much for this program, but scanf() has some
potential problems. A good way to handle more complex input is to use
fgets() (*not* gets()!) to read a line at a time, then parse it using
sscanf().
--
Keith Thompson (The_Other_Keith)
ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.