0

At various places in my code, I set certain stream properties, such as in std::cout << fixed << 4.56342;, in order to manipulate how an integer or double appears when printed to standard out. Sometimes during a particular runtime flow, std::cout is used without any manipulations but the output is inadvertently transformed because of a preceding manipulation.

So, what is the best way to reset all such properties of std::cout so that a call to std::cout<< will behave precisely as in the following example:

#include <iostream>

int main(int argc, char **argv) {
    // let X be an integer or a double
    std::cout << X;
    return 0;
}

I see Effective use of C++ iomanip library, and it makes sense generally to not use the manipulators directly. That will be a good strategy going forward. Still, it would be nice to know how to undo all such manipulations as I describe above.

Community
  • 1
  • 1
synaptik
  • 8,971
  • 16
  • 71
  • 98

1 Answers1

2

Use resetiosflags:

std::cout << std::resetiosflags( std::ios_base::basefield ); // clears integer manipulations
std::cout << std::resetiosflags( std::ios_base::floatfield  ); // clears floating-point manipulations
std::cout << std::resetiosflags( std::cout.flags() ); // clears all flags
template boy
  • 10,230
  • 8
  • 61
  • 97
  • `cout` could also have a `precision` and/or `width` set. (Though most things that use the `width` also reset it afterward.) – aschepler Aug 22 '13 at 02:24
  • @aschepler So would `cout.unsetf()` reset something like `std::setprecision(3)`? – synaptik Aug 22 '13 at 02:28
  • @aschepler But to deal with that I'd surmise that you'd need to save the value of the precision before initially setting it and reset it back when you're done. – template boy Aug 22 '13 at 02:28
  • @synaptik It won't. See the comment I wrote above. – template boy Aug 22 '13 at 02:29
  • @templateboy Thanks. I tried the wrong functions and it didn't work, but I never got around to mentioning that here because I decided to stop manipulation cout, and instead write functions that manipulate local stringstreams, which are then returned as strings and passed to cout. But, like I said, I do want to know how to reset cout, so thanks for updating your solution. – synaptik Aug 24 '13 at 19:24
  • std::resetiosflags( std::cout.flags() ) does not necessarily clear (or reset to default) all flags. See: http://ideone.com/Etwxky – Herbert Oct 23 '14 at 10:32
  • @Herbert Yes, it does but in your code the order of evaluation seems to be choosing to run `std::cout << std::restiosflags(...)` first instead of `std::cout << std::showpoint` first. If you partition them into two separate statements they will work: http://ideone.com/t7jKYo – template boy Oct 23 '14 at 15:13
  • Where is it stated that the any non-default value is a bit set to one? Otherwise I would not know why std::cout.flags() is a valid flag to reset all non-default options to the default. – Herbert Oct 24 '14 at 09:28
  • @Herbert Not sure what you mean by "non-default value is a bit set to one". Where do I make that assumption? – template boy Oct 24 '14 at 18:11
  • I'm assuming the mask needs to have the bits which need to be reset, set to one. Hence non-default values need to be 1 for std::resetiosflags( std::cout.flags() ) to reset these values. – Herbert Oct 27 '14 at 11:16
  • @Herbert Why would they need to be 1? The value of a bit in the `fmtflags` bitmask is implementation defined. – template boy Oct 28 '14 at 20:23
  • At the very least std::resetiosflags( std::cout.flags() ); is strange, I would expect std::resetiosflags( std::ios_base::allfields );, but I might malunderstand the meaning of a bitmask. – Herbert Oct 30 '14 at 21:18