The flags() member function - cout.flags(), sets or returns the current flag settings C++ program example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Sets or returns the current flag settings in C++ programming using cout.flags()

To show: How to set or returns the flags() member function using cout.flags() in C++ programming

 

// demonstrating the flags() member function any format flags() not specified in the argument to flags() are reset.

#include <iostream>

using namespace std;

 

void main(void)

{

long p = 2000;

double q = 0.00124345;

 

// set a new format state

cout<<"The value of flags variable is: "

<<cout.flags()<<"\n"

<<"Print long int and double in original format:\n"

<<p<<"\t"<<q<<"\n\n";

long OriginalFormat = cout.flags(ios::oct | ios::scientific);

 

// save the previous format state

cout<<"The value of the flags variable is: "

<<cout.flags()<<"\n"

<<"Print long int and double in a new format\n"

<<"specified using the flags member function:\n"

<<p<<"\t"<<q<<"\n\n";

cout.flags(OriginalFormat);

 

// restore the original format setting

cout<<"The value of the flags variable is: "

<<cout.flags()<<"\n"

<<"Print values in original format again:\n"

<<p<<"\t"<<q<<endl;

 

return;

}

 

Output example:

 

The value of flags variable is: 513

Print long int and double in original format:

2000 0.00124345

The value of the flags variable is: 12000

Print long int and double in a new format

specified using the flags member function:

3720 1.243450e-003

The value of the flags variable is: 513

Print values in original format again:

2000 0.00124345

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index