Output Questions using Printf

G

Gregc.

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
 
E

Eric Sosman

Gregc. said:
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.
 
T

TJW

Gregc. said:
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.

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 said:
}

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
 
G

Gregc.

Eric said:
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.
 
T

TJW

Gregc. said:
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
 
G

Gregc.

TJW said:
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.
 
K

Keith Thompson

TJW said:
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.)
 
T

TJW

Gregc. said:
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
 
T

TJW

Keith Thompson said:
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
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top