





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An overview of how to control the format of numeric output using C++ output streams. It covers the use of member functions and manipulators to change various member variables that affect the appearance of numbers. The document focuses on controlling the format of integers and doubles, saving and restoring stream settings, and using fixed format for neat output.
What you will learn
Typology: Lecture notes
1 / 9
This page cannot be seen from the preview
Don't miss anything!
cout << "Hello, world!" << endl;
Saving and restoring stream settings
ios::fmtflags old_settings = cout.flags();
cout.flags(old_settings);
int old_precision = cout.precision();
cout.precision(old_precision);
Controlling minimum field width
cout << "" << setw(4) << 12 << "" << endl; // manipulator
cout.setf(ios::fixed, ios::floatfield);
cout << fixed;
cout.setf(0, ios::floatfield);
An Example of Controlling Numerical Output Format
#include
13 2.90368e+ 14 9.12217e+ 15 2.86581e+ Each output double value has either six significant digits or fewer if the value can be expressed just as accurately. For example, for ipow = 0, the value of one shows with no decimal places and not even a decimal point. These are not printed in the default format unless there are non-zero places printed to the right of the decimal point. See also ipow = 10 through 12, where the decimal places have all been rounded off. At i = 13 and beyond, 6 digits are not enough, so the output flips into scientific notation, still showing six significant digits, but with an exponent of ten. A different rule, not so easy to state, governs when small values flip into scientific notation. /!! ! // save the current settings ! ios::fmtflags old_settings = cout.flags(); //save previous format flags ! int old_precision = cout.precision();! // save previous precision setting ! // don't need to save width setting, because it automatically resets to default value ! // after each numeric output item. ! ! // just change the precision ! cout << setprecision(8); ! cout << "\nOutput using integers with default output" << endl; ! cout << "doubles in general format, precision 8" << endl; ! for (int i = 0; i < nvalues; i++) !! cout << ipow[i] << ' ' << ary[i] << endl; ! / Output using integers with default output doubles in general format, precision 8 -10 1.0678279e- -9 3.3546804e- -8 0. -7 0. -6 0. -5 0. -4 0. -3 0. -2 0. -1 0. 0 1 1 3. 2 9. 3 31.
is messed up because the results will not fit into the total space of 8 characters (the decimal point counts as one space). Note that setting fixed format prevents the flipping into scientific notation, and forces the value of exactly one to show with a decimal point and the specified number of places to the right of the decimal point. / ! cout << "\nOutput using integers with width 3 integers, " << endl; ! cout << "doubles in fixed format, precision 0, width 5" << endl; ! // can use manipulators to change precision and others inside an output statement ! // in fixed format, precision of zero means no decimal places ! for (int i = 0; i < nvalues; i++) ! cout << setw(3) << ipow[i] << ' ' << setprecision(0) << setw(5) << ary[i] << endl; / Output using integers with width 3 integers, doubles in fixed format, precision 0, width 5 -10 0 -9 0 -8 0 -7 0 -6 0 -5 0 -4 0 -3 0 -2 0 -1 0 0 1 1 3 2 10 3 31 4 97 5 306 6 961 7 3020 8 9489 9 29809 10 93648 11 294204 12 924269 13 2903677 14 9122171 15 28658145 This gives room for the negative integer values, and so it produces a neat output until ipow = 11, whereupon the output takes additional digits just as in the previous example. Because the fixed precision is zero, everything is rounded to the nearest integer value, and thus neither a decimal point nor places to the right of the decimal point appear. For values less the one, of course, the result rounds off to zero. / ! cout << "\nOutput using integers with width 3 integers, " << endl; ! cout << "doubles in fixed format, precision 8, width 18" << endl; ! cout << setprecision(8); ! for (int i = 0; i < nvalues; i++) !! cout << setw(3) << ipow[i] << ' ' << setw(18) << ary[i] << endl; !! / Output using integers with width 3 integers, doubles in fixed format, precision 8, width 18 -10 0. -9 0.
This output is the first that is completely neat over the whole range of values. The width for the doubles leaves enough room for the additional digits to the left of the decimal point. Of course, the precision of 8 produces a lots of decimal places which we may not need. /! !! ! // restore output format flags and precision ! cout.flags(old_settings); ! cout.precision(old_precision); ! cout << "\nOutput using original settings" << endl; ! for (int i = 0; i < nvalues; i++) !! cout << ipow[i] << ' ' << ary[i] << endl; / Output using original settings -10 1.06783e- -9 3.35468e- -8 0. -7 0. -6 0. -5 0. -4 0. -3 0. -2 0. -1 0. 0 1 1 3. 2 9. 3 31. 4 97. 5 306. 6 961. 7 3020. 8 9488. 9 29809.