0

I want to turn all flags on with a loop.I tried to do it like this,

#include<iostream>
using namespace std;

void showflags()
{
   // ios::fmtflags f;
    long f=cout.flags();
    long i;
    for(i=16384;i;i=i>>1)
        if(i&f)
            cout<<"1 ";
        else cout<<"0 ";
    cout<<endl;
}

void setallflag()
{
    ios_base::fmtflags f,i=16384;
    for(;i;i>>=1)
        f=f|i;
    cout.flag(f);
}

main()
{
    showflags();
    setallflag();
    showflags();
}

but it gives me an error saying "invalid conversion from int to std::ios_base::fmtflags.

I want to know why this error occurs and how to fix it.

Reshad
  • 220
  • 5
  • 19
  • Probable future duplicate of [I never initialized `f` and everything is wrong](http://stackoverflow.com/questions/1597405/what-happens-to-a-declared-uninitialized-variable-in-c-does-it-have-a-value) – Weak to Enuma Elish Feb 21 '16 at 22:36
  • 1
    You are treading on dangerous ground here. The values of the format flags are [implementation-defined](http://en.cppreference.com/w/cpp/io/ios_base/fmtflags), so you should not attempt to iterate in this fashion since it isn't guaranteed to work anywhere except your particular compiler version/OS/architecture. You'd be better off being explicit about what flags you actually want. – mindriot Feb 21 '16 at 22:57
  • 1
    You **cannot** turn on all of the flags. For example, `dec`, `oct`, and `hex` conflict with one another with regard to integer I/O, as do `fixed`, `scientific`, and `hexfloat` for floating point I/O. – David Hammen Feb 22 '16 at 00:57

1 Answers1

2

How to turn all flags on?

You cannot turn all of the I/O flags on. Some of the I/O flags conflict with one another. For example, only one of std::oct, std::dec, and std::hex can be active. The same applies to std::fixed, std::scientific, and std::hexfloat. The only operations guaranteed by the standard are setting individual flags. This might well mean that previously set flags become disabled on a new call that sets an I/O flag. (It must mean that in the case of std::oct, std::dec, or std::hex.)

To make matters worse, the implementation of std::ios_base::fmtflags is implementation-defined. This means that an implementation could, for example, use a 64 bit implementation for the format flags, with a lot of spacing between values. Or maybe not.

David Hammen
  • 32,454
  • 9
  • 60
  • 108
  • You are saying "cout.setf(ios::uppercase|ios::boolalpha|ios::hex|ios::oct|ios::dec|ios::fixed|ios::left|ios::right|ios::showpos|ios::showpoint|ios::showbase|ios::unitbuf|ios::scientific|ios::internal);" this is not possible ,but I have written a program to know which flags are on and with this line it shows the expected output. – Reshad Feb 22 '16 at 03:16
  • @Reshad -- I am saying the exact opposite. The standard does not support `output_stream::setf (option_A | option_B)`, let alone `output_stream::setf (option_A | option_B | ... | option_Z)`. The only thing the standard supports is `output_stream::setf (specific_option)`, where `specific_option` represents one of the single bit flags mentioned in the standard. – David Hammen Feb 22 '16 at 03:23
  • the error showed in the line where I tried to assign the value of i in setallflags function,why is that. – Reshad Feb 22 '16 at 03:31
  • @Reshad - You got an error because you tried to treat `std::ios_base::fmtflags` as if is declared as `typedef int std::ios_base::fmtflags`. That isn't the case with your compiler. You can "set all flags" with the one-liner `std::cout.flags(std::ios_base::fmtflags(-1ULL));` but good luck with what that even means. – David Hammen Feb 22 '16 at 09:33