code problem

Y

yanyo

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..
 
O

osmium

yanyo said:
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;

12 spelled that way is an int. int/int yields a quotient and the remainder
is discarded. Change it to 12. which will make it a double.
 
Y

yanyo

i tried that..changed it to double, float ..also change the scanif to %lf
but nothing ...
 
S

Stan Milam

osmium said:
:




12 spelled that way is an int. int/int yields a quotient and the remainder
is discarded. Change it to 12. which will make it a double.

Partly true. gas_price is a float in the expression and all of the
operands are changed to type double, and the result is type double
converted to float on assignment to total_cost.
 
O

osmium

yanyo said:
i tried that..changed it to double, float ..also change the scanif to %lf
but nothing ...

You can't compute total cost before you have the prices and so on. Move the
total cost statement to just before the print statement. It looks like you
may be *thinking* of writing a function. But that isn't what you actually
wrote.
 
J

Jack Klein

Partly true. gas_price is a float in the expression and all of the
operands are changed to type double, and the result is type double
converted to float on assignment to total_cost.

And you answer is partly true. 'gas_price' is a float, so any ints in
an expression with it are promoted to float, not double.

Compilers are allowed to perform floating point math with more
precision than the types of the operands, so the expression _might_ be
performed at the precision of a double on some implementations.

In any case, the result of the expression is a float, and _not_ a
double, no matter what precision the calculations are actually done
in. No conversion is performed when assigning the result to a float,
none is needed.

The requirement that all float operands are promoted to double and all
floating point calculations are performed at double precision
disappeared from C in 1989/1999.
 
K

Keith Thompson

yanyo said:
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().
 
K

Kenneth Brody

yanyo said:
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;
[... snip code which gets values for gastank, gas_price, etc. ...]
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..

It would help if you calculated total_cost _after_ getting the values of
the other variables.

You'll also want to make sure that the scanf format specifiers match the
variable type. (For example, you use "%f" for gastank, which is an int.)

Finally, you should use floats (or doubles) or everything, or at least
cast them as such, to prevent integer arithmetic from losing precision.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 
B

Barry Schwarz

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;

What is the current value of the variables on the right side of the
equal sign?
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);

What type is gastank? What type did you tell scanf it is?
printf("What is the price of gasoline per gallon?\n");
scanf("%d", &gas_price);

What type is gas_price? Why did you lie to scanf again?
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..


<<Remove the del for email>>
 
D

Dave Thompson

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().

Or for simple cases like this, strtod() or strtol(). Or in some
other cases, direct code, strtok[_r](), strspn(), etc., etc.

Also, %1.2f is silly. The .2 specifies two places after the decimal
point, but the 1 does _not_ specify one place before; it specifies a
_minimum_ width of 1 for the _entire_ value. Since .nn is already 3,
any possible formatted value already exceeds the minimum. The OP
probably wants %.2f, as there's no obvious reason here for padding.

- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,459
Latest member
Vida00R129

Latest Threads

Top