Manipulators l manipulators are used only in input

  • Slides: 14
Download presentation
Manipulators l manipulators are used only in input and output statements l endl, fixed,

Manipulators l manipulators are used only in input and output statements l endl, fixed, showpoint, setw, and setprecision are manipulators that can be used to control output format l endl is use to terminate the current output line, and create blank lines in output 1

Output Statements SYNTAX (revised) cout << Expression. Or. Manipulator. . . ; 2

Output Statements SYNTAX (revised) cout << Expression. Or. Manipulator. . . ; 2

Using Manipulators Fixed and Showpoint l use the following statement to specify that (for

Using Manipulators Fixed and Showpoint l use the following statement to specify that (for output sent to the cout stream) decimal format (not scientific notation) be used, and that a decimal point be included (even for floating values with 0 as fractional part) cout << fixed << showpoint ; 3

setprecision(n) l requires #include <iomanip> and appears in an expression using insertion operator (<<)

setprecision(n) l requires #include <iomanip> and appears in an expression using insertion operator (<<) l if fixed has already been specified, argument n determines the number of places displayed after the decimal point for floating point values l remains in effect until explicitly changed by another call to setprecision 4

What is exact output? #include <iomanip> #include <iostream> // for setw( ) and setprecision(

What is exact output? #include <iomanip> #include <iostream> // for setw( ) and setprecision( ) using namespace std; int main ( ) { float my. Number = 123. 4587 ; cout << fixed << showpoint ; // use decimal format // print decimal points cout << “Number is ” << setprecision ( 3 ) << my. Number << endl ; return 0 ; } 5

OUTPUT Number is 123. 459 value is rounded if necessary to be displayed with

OUTPUT Number is 123. 459 value is rounded if necessary to be displayed with exactly 3 places after the decimal point 6

Manipulator setw l “set width” lets us control how many character positions the next

Manipulator setw l “set width” lets us control how many character positions the next data item should occupy when it is output l setw is only formatting numbers and strings, not char type data 7

setw(n) l requires #include <iomanip> and appears in an expression using insertion operator (<<)

setw(n) l requires #include <iomanip> and appears in an expression using insertion operator (<<) l argument n is called the fieldwidth specification, and determines the number of character positions in which to display a right-justified number or string (not char data). The number of positions used is expanded if n is too narrow l “set width” affects only the very next item displayed, and is useful to align columns of output 8

What is exact output? #include <iomanip> #include <iostream> //#include <string> // for setw( )

What is exact output? #include <iomanip> #include <iostream> //#include <string> // for setw( ) using namespace std; int main ( ) { int my. Number = 123 ; int your. Number = 5 ; cout << << setw ( 10 ) << << “Mine” “Yours” << endl; my. Number your. Number << endl ; return 0 ; } 9

OUTPUT position 1234567890 Mine Yours 123 5 each is displayed right-justified and each is

OUTPUT position 1234567890 Mine Yours 123 5 each is displayed right-justified and each is located in a total of 10 positions 10

What is exact output? #include <iomanip> #include <iostream> // for setw( ) and setprecision(

What is exact output? #include <iomanip> #include <iostream> // for setw( ) and setprecision( ) using namespace std; int main ( ) { float my. Number = 123. 4 ; float your. Number = 3. 14159 ; cout << fixed << showpoint ; // use decimal format // print decimal points cout << “Numbers are: ” << setprecision ( 4 ) << endl << setw ( 10 ) << my. Number << endl << setw ( 10 ) << your. Number << endl ; return 0 ; } 11

OUTPUT 1234567890 Numbers are: 123. 4000 3. 1416 each is displayed right-justified and rounded

OUTPUT 1234567890 Numbers are: 123. 4000 3. 1416 each is displayed right-justified and rounded if necessary and each is located in a total of 10 positions with 4 places after the decimal point 12

312. 0 More Examples 4. 827 x y float x = 312. 0 ;

312. 0 More Examples 4. 827 x y float x = 312. 0 ; float y = 4. 827 ; cout << fixed << showpoint ; OUTPUT cout << setprecision ( 2 ) << setw ( 10 ) << x << endl << setw ( 10 ) << y << endl ; ’’’’ 3 1 2. 00 ’’’’’’ 4. 83 cout << setprecision ( 1 ) << setw ( 10 ) << x << endl << setw ( 10 ) << y << endl ; ’’’’’ 3 1 2. 0 ’’’’’’’ 4. 8 cout << setprecision ( 5 ) << setw ( 7 ) << x << endl << setw ( 7 ) << y << endl ; 3 1 2. 00000 4. 82700 13 13

HEADER FILE MANIPULATOR ARGUMENT TYPE EFFECT <iostream> endl none terminates output line <iostream> showpoint

HEADER FILE MANIPULATOR ARGUMENT TYPE EFFECT <iostream> endl none terminates output line <iostream> showpoint none displays decimal point <iostream> fixed none suppresses scientific notation <iomanip> setw(n) int sets fieldwidth to n positions <iomanip> setprecision(n) int sets precision to n digits 14