19.06.2013 Views

Starting Out with C++: Early Objects - EEMB DERSLER

Starting Out with C++: Early Objects - EEMB DERSLER

Starting Out with C++: Early Objects - EEMB DERSLER

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Formatting <strong>Out</strong>put 111<br />

The output created by Program 3-18, as we directed, allows a maximum of five significant<br />

digits to be displayed and is printed right justified in a field width of eight characters. However,<br />

the result is clearly not what is desired. In just a moment, we’ll look at another<br />

manipulator that provides additional control over the format of the output.<br />

The fixed Manipulator<br />

If a number is too large to print using the number of digits specified <strong>with</strong> setprecision,<br />

many systems print it in scientific notation. For example, here is the output of Program 3-18<br />

<strong>with</strong> larger numbers being input.<br />

Enter the sales for day 1: 145678.99[Enter]<br />

Enter the sales for day 2: 205614.85[Enter]<br />

Enter the sales for day 3: 198645.22[Enter]<br />

Sales Figures<br />

-------------<br />

Day 1: 1.4568e+005<br />

Day 2: 2.0561e+005<br />

Day 3: 1.9865e+005<br />

Total: 5.4994e+005<br />

To prevent this, you can use another stream manipulator, fixed, which indicates that<br />

floating-point output should be printed in fixed-point, or decimal, notation.<br />

cout

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!