473,405 Members | 2,415 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

code problem

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

Nov 15 '05 #1
10 1538
"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.
Nov 15 '05 #2
i tried that..changed it to double, float ..also change the scanif to %lf
but nothing ...

Nov 15 '05 #3
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.

Nov 15 '05 #4
"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.
Nov 15 '05 #5
On Thu, 08 Sep 2005 02:14:59 GMT, Stan Milam <st*****@swbell.net>
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
Nov 15 '05 #6
"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.
Nov 15 '05 #7
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: <mailto:Th*************@gmail.com>
Nov 15 '05 #8
On Wed, 07 Sep 2005 21:54:42 -0400, "yanyo" <x_******@hotmail.com>
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>>
Nov 15 '05 #9
Thanks for the feedback i was able to get this going...

Nov 15 '05 #10
On Thu, 08 Sep 2005 05:17:11 GMT, Keith Thompson <ks***@mib.org>
wrote:
"yanyo" <x_******@hotmail.com> writes:
printf("The cost of gas for the month is %1.2f\n",total_cost);


<snip lots of good stuff I agree with and would have said>
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 strto[u]l(). 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
Nov 15 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

51
by: Mudge | last post by:
Please, someone, tell me why OO in PHP is better than procedural.
242
by: James Cameron | last post by:
Hi I'm developing a program and the client is worried about future reuse of the code. Say 5, 10, 15 years down the road. This will be a major factor in selecting the development language. Any...
53
by: Cardman | last post by:
Greetings, I am trying to solve a problem that has been inflicting my self created Order Forms for a long time, where the problem is that as I cannot reproduce this error myself, then it is...
67
by: Steven T. Hatton | last post by:
Some people have suggested the desire for code completion and refined edit-time error detection are an indication of incompetence on the part of the programmer who wants such features. ...
8
by: Paul Cochrane | last post by:
Hi all, I've got an application that I'm writing that autogenerates python code which I then execute with exec(). I know that this is not the best way to run things, and I'm not 100% sure as to...
8
by: Steve Jorgensen | last post by:
Hi folks, I'm posting this message because it's an issue I come up against relatively often, but I can't find any writings on the subject, and I haven't been able to figure out even what key...
2
by: Praveen K | last post by:
I have a problem in communicating between the C# and the Excel Interop objects. The problem is something as described below. I use Microsoft Office-XP PIA dll’s as these dll’s were been...
6
by: TPJ | last post by:
Help me please, because I really don't get it. I think it's some stupid mistake I make, but I just can't find it. I have been thinking about it for three days so far and I still haven't found any...
16
by: Rex | last post by:
Hi All - I have a question that I think MIGHT be of interest to a number of us developers. I am somewhat new to VIsual Studio 2005 but not new to VB. I am looking for ideas about quick and...
8
by: Andy B | last post by:
Before I do a no no on a newsgroup, I need to ask a question: What is the max number of lines of code you can/should post here before it gets too long?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.