Thread: Cannot write multiple printf 's after a floating point

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    4

    Cannot write multiple printf 's after a floating point

    Hey guys, this shouldn't be too hard fix but I can't seem to make this printf line work

    printf("THE NUMBER PI\n\To six decimal places, pi is equal to \%f" ,3.141593 "\ which is roughly \%i", 3 "\n");

    I'm not sure what the correct syntax is to start and end a new write. Any help would be great
    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,323
    I think you're missing the point of the format string. I would expect something like:
    Code:
    printf("THE NUMBER PI\nTo six decimal places, pi is equal to %f, which is roughly %i\n", 3.141593, 3);
    Noticed that I used the backslash for escaping for '\n', but removed the spurious ones that you used for the format specifications, e.g., "%f" instead of "\%f". If you wanted to print a literal '%' with a format string then use "%%" instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2014
    Posts
    4
    thanks that makes much more sense now, and it works too. you've been a great help

  4. #4
    Registered User
    Join Date
    Feb 2014
    Location
    NY
    Posts
    56
    You could also define pi and write your statement like this.

    Code:
    #define PI 3.14159265359
    
    printf("THE NUMBER PI\nTo six decimal places, pi is equal to %f.6, which is roughly %1f\n", PI, PI);
    Last edited by 3DT; 02-25-2014 at 12:04 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. floating point number printf/string formating
    By stanlvw in forum C++ Programming
    Replies: 2
    Last Post: 04-22-2009, 04:41 PM
  2. floating point help!
    By NoobieGecko in forum C Programming
    Replies: 7
    Last Post: 03-09-2008, 02:51 PM
  3. read/write a floating point value
    By champ in forum Networking/Device Communication
    Replies: 2
    Last Post: 09-14-2003, 12:09 AM
  4. fixed point / floating point
    By confuted in forum Game Programming
    Replies: 4
    Last Post: 08-13-2002, 01:25 PM
  5. Floating point faster than fixed-point
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-08-2001, 11:34 PM