Very simple problem

This is a discussion on Very simple problem within the C Programming forums, part of the General Programming Boards category; code[ printf("Type in three integers"); scanf("%lf %lf %lf", &m, &n, &o); printf("%.2f %.2f %.2f", m, n, o); [/code] Let's say ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    24

    Very simple problem

    code[
    printf("Type in three integers");
    scanf("%lf %lf %lf", &m, &n, &o);
    printf("%.2f %.2f %.2f", m, n, o);
    [/code]

    Let's say I put 1 2 -3. How do I make the output look like...
    Code:
    +1.00 +2.00 -3.00

  2. #2
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    I'm pretty sure you'd have to parse each input manually. Save each input into a string and see if it starts with a '-' or not, then you can output a '+' or '-' as necessary. You'd need to convert each string with atof() too.

    Right?
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    printf("%+.2f %+.2f %+.2f", m, n, o);
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    No but if he's taking input like "-3.00" that means your solution is assuming positive values only. Right?
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    I thought it might be something about printf() that I didn't know
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  7. #7
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    scanf("%lf %lf %lf", &m, &n, &o);
    if (m<0) printf("%.2f ",m);
    else printf("+%.2f ",m);
    if (n<0) printf("%.2f ",n);
    else printf("+%.2f ",n);
    if (o<0) printf("%.2f ",o);
    else printf("+%.2f ",o);

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Did you read the other posts? Including Dave's post and link?

    This code
    Code:
    printf("%+.2f", x);
    automatically adds a '+' or a '-' to the number. You don't need to figure it out yourself.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A simple file I/O problem
    By eecoder in forum C Programming
    Replies: 10
    Last Post: 10-17-2010, 12:00 AM
  2. Problem in simple code.
    By richdb in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 02:45 AM
  3. Problem in very simple code
    By richdb in forum C Programming
    Replies: 22
    Last Post: 01-14-2006, 09:10 PM
  4. Simple Initialization Problem
    By PsyK in forum C++ Programming
    Replies: 7
    Last Post: 04-30-2004, 08:37 PM
  5. Simple OO Problem
    By bstempi in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2004, 06:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21