formatting cout

This is a discussion on formatting cout within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by jimblumberg Actually since the default setfil() is " " you don't even need the setfill(), the same ...

  1. #16
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by jimblumberg View Post
    Actually since the default setfil() is " " you don't even need the setfill(), the same with the right, it too is the default so it isn't actually needed. The std::fixed and perhaps a setprecision(), if you want less than the default number of digits, would be all you would need.
    I put those calls in to ensure the stream was in the correct state. Obviously in the given example code it's unnecessary, but I have no idea what might be done to the stream before the code is used in the actual project. So to be on the safe side, I reinstated the requisite defaults. Perhaps there's a better way to do this? A single "reset stream to default" instruction? I haven't used C++ ostream formatting very much so I don't know.

    You also don't need to print that extra space, just use the proper setw() size to allow the proper space between the fields.
    I always put at least one space between them just in case the number fills or overfills the given width. If that happens and there's no extra space the numbers will run together.

    Also in my post #11 I was only pointing out the fact that several of the calls were unnecessary, I didn't actually try the code and wasn't commenting on what output the code would produce.
    I realize that. I learned about the "stickiness" of certain settings from your post, so thanks.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  2. #17
    Registered User
    Join Date
    May 2010
    Posts
    2,919
    So to be on the safe side, I reinstated the requisite defaults. Perhaps there's a better way to do this? A single "reset stream to default" instruction? I haven't used C++ ostream formatting very much so I don't know.
    I'm not aware of any one command to return the stream to the default state there are several ways of resetting various settings, for example setf() and unsetf() control the left, right or internal and the dec, oct or hex and scientific or fixed flags.

    Another way is to capture the previous settings before or when you set the values so you can reset the stream to those values:

    Code:
       std::cout.setf(std::ios::fixed);
       int prec = std::cout.precision(2);
       out(-1,       -1.61803,  0);
       out( 1.61803,  0,        1);
       std::cout.unsetf(std::ios::fixed);
       std::cout.precision(prec);

    Another way to get the stream back to the default setting is to create a new stream (any kind of stream will do) and copy the format from this new stream to the stream you want to change back to the default settings.

    Code:
       std::stringstream ins;
       std::cout.copyfmt(ins); // Copy ins's format specifiers to the std::cout stream.
    Jim

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cout formatting
    By disruptivetech in forum C++ Programming
    Replies: 10
    Last Post: 01-09-2008, 04:29 PM
  2. float formatting using cout
    By JJFMJR in forum C++ Programming
    Replies: 3
    Last Post: 08-11-2007, 11:31 PM
  3. Simple cout formatting question.
    By guitarist809 in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2006, 12:59 PM
  4. Whats the difference between cout and std::cout?
    By mdshort in forum C++ Programming
    Replies: 10
    Last Post: 12-30-2003, 05:34 PM
  5. formatting output with cout
    By boojus in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2003, 07:17 AM

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