1 / 13

CPS120: Introduction to Computer Science

CPS120: Introduction to Computer Science. Formatted I/O. Formatted 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)

Download Presentation

CPS120: Introduction to Computer Science

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CPS120: Introduction to Computer Science Formatted I/O

  2. Formatted 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

  3. Line 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;

  4. Escape 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

  5. Using setf and unsetf • Each stream has format options that can be changed OPTIONDESCRIPTION left Left-justifies the output right Right-justifies the output showpoint Displays decimal point and trailing zeros forfloats uppercase Displays e in scientific as E showpos Displays a leading plus sign scientific Displays floating point number scientifically fixed Displays floating-point in normal notation

  6. Using Format Options • Format options are set immediately prior to the COUT statement float x = 24.0; cout << x << ‘\n’; // displays 24 cout.setf(ios::showpoint); cout << x << ‘\n’; // displays 24.00000 cout.unsetf(ios::showpoint); cout << x << ‘\n’; // displays 24

  7. Using 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 statement cout << setprecision(2) << price << ‘\n’;

  8. Setting 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

  9. More 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

  10. Field 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

  11. Formatting Numbers for Output double price; price = 78.5; cout << "The price is $"; cout << price << endl; • We want the price to be $78.50

  12. Magic Formula for Currency cout.setf(ios :: fixed); cout.setf(ios :: showpoint); cout.precision(2);

  13. Formatted 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

More Related