Loading....
Coupon Accepted Successfully!

 

Bit Fields

The ios class contains the setf() member function. The flag indicates the format design. The syntax of the unsetf() function is used to clear the flags. The syntaxes of the function are as follows.
 

Syntax: cout.setf (v1, v2);

Syntax: cout.unsetf(v1);


where variable v1 and v2 are two flags. Table 3.5 describes the flag, and bit-field setting can be used with this function. The unsetf() accepts setting of v1 and v2 arguments.

 

Table: Flags and Bits

Format

Flag (V1)

Bit Field (V2)

Left justification

ios::left

ios:adjustfield

Right justification

ios::right

ios:adjustfield

Padding after sign and base

ios::internal

ios:adjustfield

Scientific notation

Fixed point notation

ios:: scientific

ios:: fixed

ios::floatfield

ios::floatfield

Decimal base

Octal base

Hexadecimal base

ios:: dec

ios::oct

ios::hex

ios::basefield

ios::basefield

ios::basefield

  1. ios::adjustfield
     
    It is a data member and used with setf() function to arrange padding to the left or right, or for internal fill. It can be declared as follows.
     
    static const long adjustfield;
  2. ios::floatfield
     
    It is a data member and used with setf() function to set the float point notation to scientific or fixed. It is declared as follows.
     
    static const long floatfield;
  3. ios::basefield
     
    It is a data member and used with setf() function and used with setf() function to set the notation to a decimal, octal, or hexadecimal base. It is declared as follows.
     
    static const long basefield;

3.38 Write a program to display the message left and right justified.


Explanation: In the above program, the fill character is set to sign “=”. The setf() is set to right justified. The column width is set to 20. The message “Figure” appears at the 20th column. Before the message the blank space is filled with the sign “=”. Again the justification property is set to left in setf() function. The text appears left justified. The output can be observed as follows.

 

 

 

3.39 Write a program to display the number in scientific format with sign.

Explanation: In the above program, the fill character is set to sign “=”. The setf() properties are set to internal and scientific. The scientific properties display the number in scientific (e) format. The internal properties display the sign before blank spaces. If the setting were removed, the output would be as given below.

 

Effect of statement

 

cout.setf (ios::internal, ios::adjustfield);

 

-

=

=

=

=

=

3

.

1

2

1

e

+

0

0


The output without this statement would be as follows.

 

=

=

=

=

=

-

3

.

1

2

1

e

+

0

0


The property ios::fixed displays the float number without scientific format, though the number is big. If the floating-point number is more than 6, then the extras are ignored. This format displays floating point up to 6 only.

 

3.40 Write a program to convert a decimal number to hexadecimal and octal format.

Explanation: In the above program, the properties of setf() function are set to ios::hex, ios:: oct, and ios::dec. After this setting, the decimal number given in cout() statement will convert to its hexadecimal and octal equivalent, respectively. The hexadecimal number 0xfe is converted to its decimal equivalent.





Test Your Skills Now!
Take a Quiz now
Reviewer Name