C++ Logo.wine
C++

Explain use of any two I/O formatting flags in C++ with example.

C++ defines some format flags for standard input and output, which can be manipulated with the flags, setf, and unsetf functions. For example,
  •  cout.setf(ios_base::left, ios_base::adjustfield);
  •  void stream::unsetf(fmtflags flags);
The function unsetf() uses flags to clear the io_stream_format_flags associated with the current stream.
#include <iostream.h>
main()
{
ios_base::fmtflags original_flags = cout.flags();
cout<< 812<<‘|’;
cout.setf(ios_base::left,ios_base::adjustfield);
cout.width(10);
cout<< 813 << 815 << ‘\n’;
cout.unsetf(ios_base::adjustfield);
cout.precision(2);
cout.setf(ios_base::uppercase|ios_base::scientific);
cout << 831.0 << ‘ ‘ << 8e2;
cout.flags(original_flags);
}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.