Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > code problem

Reply
Thread Tools

code problem

 
 
yanyo
Guest
Posts: n/a
 
      09-08-2005
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..

 
Reply With Quote
 
 
 
 
osmium
Guest
Posts: n/a
 
      09-08-2005
"yanyo" 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;


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.


 
Reply With Quote
 
 
 
 
yanyo
Guest
Posts: n/a
 
      09-08-2005
i tried that..changed it to double, float ..also change the scanif to %lf
but nothing ...

 
Reply With Quote
 
Stan Milam
Guest
Posts: n/a
 
      09-08-2005
osmium wrote:
> "yanyo" 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;

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

 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      09-08-2005
"yanyo" writes:

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


 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      09-08-2005
On Thu, 08 Sep 2005 02:14:59 GMT, Stan Milam <>
wrote in comp.lang.c:

> osmium wrote:
> > "yanyo" 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;

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


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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      09-08-2005
"yanyo" <> 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) kst- <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.
 
Reply With Quote
 
Kenneth Brody
Guest
Posts: n/a
 
      09-08-2005
yanyo wrote:
>
> 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: <private.php?do=newpm&u=>


 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      09-09-2005
On Wed, 07 Sep 2005 21:54:42 -0400, "yanyo" <>
wrote:

>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>>
 
Reply With Quote
 
yanyo
Guest
Posts: n/a
 
      09-12-2005
Thanks for the feedback i was able to get this going...

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with code - and code wariors thedarkman HTML 5 09-13-2010 11:40 PM
what is the difference between code inside a <script> tag and code in the code-behind file? keithb ASP .Net 1 03-29-2006 01:00 AM
Problem getting cookie from code-behind (worked in code-beside) Alan Silver ASP .Net 1 09-15-2005 05:23 PM
Problem with ejbdoclet--generated bean code is inheriting from "phantom" code... mwkohout@gmail.com Java 0 02-14-2005 06:06 PM
Fire Code behind code AND Javascript code associated to a Button Click Event =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?= ASP .Net 4 02-11-2004 07:31 AM



Advertisments