Hi
Sorry to bother you gusy with such a basic question, but I am working
on a conversion program. I've got most of it down, but I am having
trouble with the output:
*Dollar Conversion Problem*/
/*--------------------------------------------------------------------------------------------------------------------*/
/*Head Files*/
/*-------------------------------------------------------------------------------------------------------------------*/
#include <stdio.h>
/*-------------------------------------------------------------------------------------------------------------------*/
/*Main*/
/*----------------------------------------------------------------------------------------------------------------------*/
main()
{
float coins,value,rate,ounce,us,aus;
printf("%s","Please Enter Number of Coins:");
scanf("%6.2f",&coins);
printf("%s","Amount of Gold in a Coin(In Ounces):");
scanf("%f",&ounce);
printf("%s","Value of Gold per Ounce USD:");
scanf("%f",&value);
printf("%s","Conversion Rate Between AUD and USD:");
scanf("%6.2f",&rate);
us=coins*ounce*value;
aus=us/rate;
printf("\n%s%6.2f%s%6.2f","USD is$:",us," AUD is$:",aus);
}
Could someone point me in the correct direction.
Thanks 8 4797
Gregc. wrote: Hi
Sorry to bother you gusy with such a basic question, but I am working on a conversion program. I've got most of it down, but I am having trouble with the output:
*Dollar Conversion Problem*/ /*--------------------------------------------------------------------------------------------------------------------*/ /*Head Files*/ /*-------------------------------------------------------------------------------------------------------------------*/ #include <stdio.h> /*-------------------------------------------------------------------------------------------------------------------*/ /*Main*/ /*----------------------------------------------------------------------------------------------------------------------*/ main() { float coins,value,rate,ounce,us,aus; printf("%s","Please Enter Number of Coins:");
Just a warning: On some C implementations, the output
written to stdout is "line buffered" and may not actually
appear until a complete line has been output. To be as sure
as possible that your incomplete-line prompts will show up,
insert `fflush(stdout);' before attempting input. (There's
another way of handling this, but let's leave the advanced
topics for another time.)
scanf("%6.2f",&coins);
Despite a few surface similarities, scanf() formats are
not like printf() formats. "%6.2f" is not a valid specifier,
and even "%6f" is dubious albeit legitimate. I strongly
recommend a plain "%f" here and in similar spots.
printf("%s","Amount of Gold in a Coin(In Ounces):"); scanf("%f",&ounce); printf("%s","Value of Gold per Ounce USD:"); scanf("%f",&value); printf("%s","Conversion Rate Between AUD and USD:"); scanf("%6.2f",&rate); us=coins*ounce*value; aus=us/rate;
printf("\n%s%6.2f%s%6.2f","USD is$:",us," AUD is$:",aus);
This could be written more simply as
printf ("\nUSD is$:%6.2f AUD is$:%6.2f", us, aus);
.... and as before, incomplete lines might not appear until
you call fflush(). A more customary approach would be to
output a newline character
printf ("\nUSD... \n", us, aus);
}
Could someone point me in the correct direction.
It's difficult to know just where your compass has gone
awry: You haven't told us how your program's behavior differs
from what you wanted.
--
Eric Sosman es*****@acm-dot-org.invalid
"Gregc." <gr*********@bigpond.com> writes: Hi
Sorry to bother you gusy with such a basic question, but I am working on a conversion program. I've got most of it down, but I am having trouble with the output:
Please describe exactly what the trouble is. What do you expect
to happen and what is actually happening? I'll take a stab at what
I think is wrong, and make some points.
<snipped headers> main()
main() returns an integer.
{ float coins,value,rate,ounce,us,aus; printf("%s","Please Enter Number of Coins:");
Can you really have a fractional number of coins? Should coins be
declared an integer type?
scanf("%6.2f",&coins);
Remove the 6.2, or better, declare coins of type int and change
this to %d
printf("%s","Amount of Gold in a Coin(In Ounces):"); scanf("%f",&ounce); printf("%s","Value of Gold per Ounce USD:"); scanf("%f",&value); printf("%s","Conversion Rate Between AUD and USD:"); scanf("%6.2f",&rate);
Remove the 6.2 ...
us=coins*ounce*value;
If coins is an int, cast it to a float just to be explicit.
aus=us/rate;
printf("\n%s%6.2f%s%6.2f","USD is$:",us," AUD is$:",aus);
Again, main() returns an integer. At a minimum add return 0
here. Better (more portable) use #include <stdlib.h> and return (EXIT_SUCCESS);
}
Could someone point me in the correct direction.
With the above changes, I compiled with seemingly sane
results. It is a place to start, but there is still room for
improvement.
Good Luck,
-TJW
Eric Sosman wrote: It's difficult to know just where your compass has gone awry: You haven't told us how your program's behavior differs from what you wanted.
--
My output should be: USD948.35 and $AU1334.77 but instead I am getting
USD is$:0.00 AUD is$:Inf. I think it has something to do with the
printf statement.
"Gregc." <gr*********@bigpond.com> writes: Eric Sosman wrote: It's difficult to know just where your compass has gone awry: You haven't told us how your program's behavior differs from what you wanted.
--
My output should be: USD948.35 and $AU1334.77 but instead I am getting USD is$:0.00 AUD is$:Inf. I think it has something to do with the printf statement.
On what input? Have you tried to print out the values you are
reading in?
Good Luck,
-TJW
TJW wrote: -- My output should be: USD948.35 and $AU1334.77 but instead I am getting USD is$:0.00 AUD is$:Inf. I think it has something to do with the printf statement.
On what input? Have you tried to print out the values you are reading in?
How do you do that? I'm inputting 5 coins, 0.5159 ounces, 367.65 per
ounce and the value of the AUD is .7105.
TJW <tj********@nomail.org> writes:
[...] Again, main() returns an integer. At a minimum add return 0 here. Better (more portable) use #include <stdlib.h> and return (EXIT_SUCCESS);
A "return 0;" at the end of main() is perfectly portable. The
portable values you can return from main() are 0, EXIT_SUCCESS, and
EXIT_FAILURE. 0 and EXIT_SUCCESS cause the program to return some
implementation-defined status (probably, but not necessarily, the same
one) that indicates successful termination; EXIT_FAILURE denotes
failure.
(In C99, falling off the end of main() is equivalent to executing
"return 0;"; I don't recommend using this feature.)
--
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.
"Gregc." <gr*********@bigpond.com> writes: TJW wrote: >> -- > My output should be: USD948.35 and $AU1334.77 but instead I am getting > USD is$:0.00 AUD is$:Inf. I think it has something to do with the > printf statement.
On what input? Have you tried to print out the values you are reading in?
How do you do that? I'm inputting 5 coins, 0.5159 ounces, 367.65 per ounce and the value of the AUD is .7105.
When you are first starting out, I find that it is a good idea to
follow each scanf with another printf, printing the value that you
just read in. Applying the changes Eric Sosman or I suggested
(in my previous post), I produced the values you were expecting.
Good Luck,
-TJW
Keith Thompson <ks***@mib.org> writes: A "return 0;" at the end of main() is perfectly portable. The portable values you can return from main() are 0, EXIT_SUCCESS, and EXIT_FAILURE. 0 and EXIT_SUCCESS cause the program to return some implementation-defined status (probably, but not necessarily, the same one) that indicates successful termination; EXIT_FAILURE denotes failure.
Noted. I always explain that awkwardly ... thanks for the
clarification.
-TJW This discussion thread is closed Replies have been disabled for this discussion. Similar topics
5 posts
views
Thread by Abe Simpson |
last post: by
|
12 posts
views
Thread by Matt Garman |
last post: by
|
3 posts
views
Thread by Paul Watson |
last post: by
|
reply
views
Thread by newbie |
last post: by
|
6 posts
views
Thread by Allan M. Bruce |
last post: by
| |
11 posts
views
Thread by aljaber |
last post: by
| | | | | | | | | | | | |