473,218 Members | 1,439 Online

# Output Questions using Printf

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*/
/*--------------------------------------------------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------------------------------------------------*/
#include <stdio.h>
/*-------------------------------------------------------------------------------------------------------------------*/
/*Main*/
/*----------------------------------------------------------------------------------------------------------------------*/
main()
{
float coins,value,rate,ounce,us,aus;
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

Apr 29 '06 #1
8 4939
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*/
/*--------------------------------------------------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------------------------------------------------*/
#include <stdio.h>
/*-------------------------------------------------------------------------------------------------------------------*/
/*Main*/
/*----------------------------------------------------------------------------------------------------------------------*/
main()
{
float coins,value,rate,ounce,us,aus;
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
Apr 29 '06 #2
"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
Apr 29 '06 #3

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.

Apr 29 '06 #4
"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

Good Luck,
-TJW
Apr 29 '06 #5

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

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.

Apr 29 '06 #6
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.
Apr 29 '06 #7
"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

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
Apr 29 '06 #8

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
Apr 29 '06 #9

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