cout << "\"Furry Friends\" is $" << price1 << "!\n";

    cout << "\"Fiery Fiends\" is $" << price2 << "!\n";

    return 0;

}

Here is the output of the program in Listing 17.6:

"Furry Friends" is $20.4!

"Fiery Fiends" is $2.78889!

"Furry Friends" is $20!

"Fiery Fiends" is $2.8!

Note that the third line of this output doesn’t include a trailing decimal point. Also the fourth line displays a total of two digits.

Printing Trailing Zeros and Decimal Points

Certain forms of output, such as prices or numbers in columns, look better if trailing zeros are retained. For example, the output to Listing 17.6 would look better as $20.40 than as $20.4. The iostream family of classes doesn’t provide a function whose sole purpose is to accomplish that. However, the ios_base class provides a setf() (for set flag) function that controls several formatting features. The class also defines several constants that can be used as arguments to this function. For example, the following function call causes cout to display trailing decimal points:

cout.setf(ios_base::showpoint);

In the default floating-point format, it also causes trailing zeros to be displayed. That is, instead of displaying 2.00 as 2, cout will display it as 2.00000 if the default precision of 6 is in effect. Listing 17.7 adds this statement to Listing 17.6.

In case you’re wondering about the notation ios_base::showpoint, showpoint is a class-scope static constant that is defined in the ios_base class declaration. Class scope means that you have to use the scope-resolution operator (::) with the constant name if you use the name outside a member function definition. So ios_base::showpoint names a constant defined in the ios_base class.

Listing 17.7. showpt.cpp

// showpt.cpp -- setting the precision, showing trailing point

#include

int main()

{

    using std::cout;

    using std::ios_base;

    float price1 = 20.40;

    float price2 = 1.9 + 8.0 / 9.0;

    cout.setf(ios_base::showpoint);

    cout << "\"Furry Friends\" is $" << price1 << "!\n";

    cout << "\"Fiery Fiends\" is $" << price2 << "!\n";

    cout.precision(2);

    cout << "\"Furry Friends\" is $" << price1 << "!\n";

    cout << "\"Fiery Fiends\" is $" << price2 << "!\n";

    return 0;

}

Here is the output of the program in Listing 17.7, using the current C++ formatting:

"Furry Friends" is $20.4000!

"Fiery Fiends" is $2.78889!

"Furry Friends" is $20.!

"Fiery Fiends" is $2.8!

This output shows the trailing zeros for the first line. The third line shows the decimal point but no trailing zeros because the precision has been set to 2 and two digits already have been displayed.

More About setf()

The setf() method controls several other formatting choices besides when the decimal point is displayed, so let’s take a closer look at it. The ios_base class has a protected data member in which individual bits (called flags in this context) control different formatting aspects, such as the number base and whether trailing zeros are displayed. Turning a flag on is called setting the flag (or bit) and means setting the bit to 1. (Bit flags are the programming equivalent to setting DIP switches to configure computer hardware.) The hex, dec, and oct manipulators, for example, adjust the three flag bits that control the number base. The setf() function provides another means of adjusting flag bits.

The setf() function has two prototypes. The first is this:

fmtflags setf(fmtflags);

Here fmtflags is a typedef name for a bitmask type (see the following Note) used to hold the format flags. The name is defined in the ios_base class. This version of setf() is used for setting format information controlled by a single bit. The argument is a fmtflags value that indicates which bit to set. The return value is a type fmtflags number that indicates the former settings of all the flags. You can then save that value if you later want to restore the original settings. What value do you pass to setf()? If you want to set bit number 11 to 1, you pass a number that has its number 11 bit set to 1. The return value would have its number 11 bit assigned the prior value for that bit. Keeping track of bits sounds (and is) tedious. However, you don’t have to do that job; the ios_base class defines constants that represent the bit values. Table 17.1 shows some of these definitions.

Table 17.1. Formatting Constants

Note

A bitmask type is a type that is used to store individual bit values. It could be an integer type, an enum, or an STL bitset container. The main idea is that each bit is individually accessible and has its own meaning. The iostream package uses bitmask types to store state information.

Перейти на страницу:

Все книги серии Developer's Library

Похожие книги