Output Questions using Printf

Discussion in 'C Programming' started by Gregc., Apr 29, 2006.

  1. Gregc.

    Gregc. Guest

    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
     
    Gregc., Apr 29, 2006
    #1
    1. Advertising

  2. Gregc.

    Eric Sosman Guest

    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
    lid
     
    Eric Sosman, Apr 29, 2006
    #2
    1. Advertising

  3. Gregc.

    TJW Guest

    "Gregc." <> 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
     
    TJW, Apr 29, 2006
    #3
  4. Gregc.

    Gregc. Guest

    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., Apr 29, 2006
    #4
  5. Gregc.

    TJW Guest

    "Gregc." <> 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, Apr 29, 2006
    #5
  6. Gregc.

    Gregc. Guest

    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.
     
    Gregc., Apr 29, 2006
    #6
  7. TJW <> 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) <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.
     
    Keith Thompson, Apr 29, 2006
    #7
  8. Gregc.

    TJW Guest

    "Gregc." <> 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
     
    TJW, Apr 29, 2006
    #8
  9. Gregc.

    TJW Guest

    Keith Thompson <> 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
     
    TJW, Apr 29, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ben
    Replies:
    4
    Views:
    657
    Martin Ambuhl
    Jun 26, 2004
  2. whatluo

    (void) printf vs printf

    whatluo, May 26, 2005, in forum: C Programming
    Replies:
    29
    Views:
    1,328
  3. benn
    Replies:
    6
    Views:
    585
    Vladimir Oka
    Mar 5, 2008
  4. azza

    printf affects following printf/s

    azza, Oct 17, 2010, in forum: C Programming
    Replies:
    0
    Views:
    455
  5. guru
    Replies:
    8
    Views:
    301
Loading...

Share This Page