cps120: introduction to computer science formatted i/o
TRANSCRIPT
![Page 1: CPS120: Introduction to Computer Science Formatted I/O](https://reader031.vdocuments.us/reader031/viewer/2022020111/56649f395503460f94c563d4/html5/thumbnails/1.jpg)
CPS120: Introduction to Computer ScienceCPS120: Introduction to Computer Science
Formatted I/O
![Page 2: CPS120: Introduction to Computer Science Formatted I/O](https://reader031.vdocuments.us/reader031/viewer/2022020111/56649f395503460f94c563d4/html5/thumbnails/2.jpg)
Formatted Output -- CurrencyFormatted Output -- Currency
cout.setf(ios : : fixed) Print “fixed point” form, not in exponential form
cout.setf(ios : : showpoint) Says to always print the decimal point
cout.precison(2) Says to print out the two most significant decimal
digits, after rounding to this precision
![Page 3: CPS120: Introduction to Computer Science Formatted I/O](https://reader031.vdocuments.us/reader031/viewer/2022020111/56649f395503460f94c563d4/html5/thumbnails/3.jpg)
Line SpacingLine Spacing In order to end a line in an output statement you may
use the new line character, \n, instead of endl. Examples:
cout << "Hello world" << '\n';cout << "Hello world" << "\n";cout << "Hello world\n";
These are practically equivalent to: cout << "Hello world" << endl;
![Page 4: CPS120: Introduction to Computer Science Formatted I/O](https://reader031.vdocuments.us/reader031/viewer/2022020111/56649f395503460f94c563d4/html5/thumbnails/4.jpg)
Escape SequencesEscape Sequences
Other useful "escape sequences" (since the \ is the escape operator) are: \t to generate a tab\\ to print a backslash\' to print a single quote\" to print a double quote
![Page 5: CPS120: Introduction to Computer Science Formatted I/O](https://reader031.vdocuments.us/reader031/viewer/2022020111/56649f395503460f94c563d4/html5/thumbnails/5.jpg)
Using setf and unsetfUsing setf and unsetf Each stream has format options that can be changedOPTION DESCRIPTIONleft Left-justifies the outputright Right-justifies the outputshowpoint Displays decimal point and trailing zeros for
floatsuppercase Displays e in scientific as Eshowpos Displays a leading plus signscientific Displays floating point number scientificallyfixed Displays floating-point in normal notation
![Page 6: CPS120: Introduction to Computer Science Formatted I/O](https://reader031.vdocuments.us/reader031/viewer/2022020111/56649f395503460f94c563d4/html5/thumbnails/6.jpg)
Using Format OptionsUsing Format Options
Format options are set immediately prior to the COUT statement
float x = 24.0;cout << x << ‘\n’; // displays 24cout.setf(ios::showpoint);cout << x << ‘\n’; // displays 24.00000cout.unsetf(ios::showpoint);cout << x << ‘\n’; // displays 24
![Page 7: CPS120: Introduction to Computer Science Formatted I/O](https://reader031.vdocuments.us/reader031/viewer/2022020111/56649f395503460f94c563d4/html5/thumbnails/7.jpg)
Using ManipulatorsUsing Manipulators You must include the <iomanip.h> header file at
the top of your program in order to use the setprecision, setw, and other manipulators. You must use place the following compiler directive at the top of your program.#include <iomanip.h>
I/O manipulators are placed directly in the output statementcout << setprecision(2) << price << ‘\n’;
![Page 8: CPS120: Introduction to Computer Science Formatted I/O](https://reader031.vdocuments.us/reader031/viewer/2022020111/56649f395503460f94c563d4/html5/thumbnails/8.jpg)
Setting PrecisionSetting Precision
The setprecision manipulator allows you to limit the number of digits that are displayed when a numeric data type is displayed:
cout << setprecision(2) << price << '\n'; only allows the leading two digits of the value stored in the variable, price, to be displayed
![Page 9: CPS120: Introduction to Computer Science Formatted I/O](https://reader031.vdocuments.us/reader031/viewer/2022020111/56649f395503460f94c563d4/html5/thumbnails/9.jpg)
More PreciselyMore Precisely
If the fixed format was set previously with the statement:
cout.setf(ios::fixed);then the setprecision(2) manipulator would have the effect of rounding or truncating price (and all future floating-point values in the cout stream) to the hundredths place
![Page 10: CPS120: Introduction to Computer Science Formatted I/O](https://reader031.vdocuments.us/reader031/viewer/2022020111/56649f395503460f94c563d4/html5/thumbnails/10.jpg)
Field WidthField Width
The setw manipulator controls the width of the field when displaying a value. The statement:
cout << setw(10) << umEndow << endl;
sets the width of the field allocated for the variable, umEndow, to 10 characters
![Page 11: CPS120: Introduction to Computer Science Formatted I/O](https://reader031.vdocuments.us/reader031/viewer/2022020111/56649f395503460f94c563d4/html5/thumbnails/11.jpg)
Formatting Numbers for OutputFormatting Numbers for Output
double price;price = 78.5;cout << "The price is $";cout << price << endl;
We want the price to be $78.50
![Page 12: CPS120: Introduction to Computer Science Formatted I/O](https://reader031.vdocuments.us/reader031/viewer/2022020111/56649f395503460f94c563d4/html5/thumbnails/12.jpg)
Magic Formula for CurrencyMagic Formula for Currency
cout.setf(ios :: fixed);cout.setf(ios :: showpoint);cout.precision(2);
![Page 13: CPS120: Introduction to Computer Science Formatted I/O](https://reader031.vdocuments.us/reader031/viewer/2022020111/56649f395503460f94c563d4/html5/thumbnails/13.jpg)
Formatted Output -- CurrencyFormatted Output -- Currency
cout.setf(ios : : fixed) Print “fixed point” form, not in exponential form
cout.setf(ios : : showpoint) Says to always print the decimal point
cout.precison(2) Says to print out the two most significant decimal
digits, after rounding to this precision