473,218 Members | 1,439 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,218 software developers and data experts.

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*/
/*--------------------------------------------------------------------------------------------------------------------*/
/*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

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*/
/*--------------------------------------------------------------------------------------------------------------------*/
/*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
Apr 29 '06 #2
TJW
"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
TJW
"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
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
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.

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

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

Similar topics

5
by: Abe Simpson | last post by:
Hello all, The application I am working on must never output numbers in a floating-point format, that is, something like 2e-002 is a big no-no. At the same time, it must output numbers in a...
12
by: Matt Garman | last post by:
I'd like to create a "custom output facility". In other words, I want an object whose use is similar to std::cout/std::cerr, but offers more flexibility. Instead of simply writing the parameter...
3
by: Paul Watson | last post by:
It is clear that just using 'print' with variable names is relatively uncontrollable. However, I thought that using a format string would reign the problem in and give the desired output. Must...
0
by: newbie | last post by:
i'm a newbie of c language. can anyone help me to implement the code so that I can get the ciphertext from the output. thanks. #ifndef _3DES_H #define _3DES_H #ifndef uint8 #define uint8 ...
6
by: Allan M. Bruce | last post by:
I just recently took a C programming test (which was a total farce - but thats not the point). The code below is a combination of two questions that were included in the test and I was hoping to...
4
by: Thiengineer | last post by:
Given the following code in C: char *s = {"program","test","load","frame","stack",NULL}; char **p = s + 2; Questions:
11
by: aljaber | last post by:
hi, i am facing a problem with my program output here is the program /*********************************************\ * CD Database * * ...
1
by: sonal sharma | last post by:
main() { int x=0; { int x=10; x++; change_value(x); x++; Modify_value(); printf("First output: %d\n",x);
1
by: yohan610 | last post by:
i have to read the binary data of a file, then encrypt them according to a supplied algorithm...and then the obtained output has to be written to an output file...everything works ok, and there are...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.