C++ Stream Manipulators: Formatting Input/Output Operations, Slides of Computer Science

An outline of c++ stream manipulators, including their capabilities, integral stream base, floating-point precision, field width, user-defined manipulators, stream format states, and error states. It covers various manipulators like dec, oct, hex, setbase, scientific, fixed, showpoint, left, right, internal, adjustfield, fill, setfill, and their functions. The document also includes examples of using these manipulators.

Typology: Slides

2012/2013

Uploaded on 03/21/2013

dharmaraaj
dharmaraaj šŸ‡®šŸ‡³

4.4

(68)

145 documents

1 / 35

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Outline (continued)
21.6 Stream Manipulators
21.6.1 Integral Stream Base: dec, oct, hex and setbase
21.6.2 Floating-Point Precision (precision, setprecision)
21.6.3 Field Width (setw, width)
21.6.4 User-Defined Manipulators
21.7 Stream Format States
21.7.1 Format State Flags
21.7.2 Trailing Zeros and Decimal Points (ios::showpoint)
21.7.3 Justification (ios::left, ios::right, ios::internal)
21.7.4 Padding (fill, setfill)
21.7.5 Integral Stream Base (ios::dec, ios::oct, ios::hex,
ios::showbase)
21.7.6 Floating-Point Numbers; Scientific Notation
(ios::scientific, ios::fixed)
21.7.7 Uppercase/Lowercase Control (ios::uppercase)
21.7.8 Setting and Resetting the Format Flags (flags,
setiosflags, resetiosflags)
21.8 Stream Error States
21.9 Tying an Output Stream to an Input Stream
Chapter 21 - C++ Stream Input/Output Stream
Manipulators
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23

Partial preview of the text

Download C++ Stream Manipulators: Formatting Input/Output Operations and more Slides Computer Science in PDF only on Docsity!

Outline (continued)

21.6 Stream Manipulators

21.6.1 Integral Stream Base: dec , oct , hex and setbase

21.6.2 Floating-Point Precision ( precision , setprecision )

21.6.3 Field Width ( setw , width )

21.6.4 User-Defined Manipulators

21.7 Stream Format States

21.7.1 Format State Flags

21.7.2 Trailing Zeros and Decimal Points ( ios::showpoint )

21.7.3 Justification ( ios::left , ios::right , ios::internal )

21.7.4 Padding ( fill , setfill )

21.7.5 Integral Stream Base ( ios::dec , ios::oct , ios::hex ,

ios::showbase )

21.7.6 Floating-Point Numbers; Scientific Notation

( ios::scientific , ios::fixed )

21.7.7 Uppercase/Lowercase Control ( ios::uppercase )

21.7.8 Setting and Resetting the Format Flags ( flags ,

setiosflags , resetiosflags )

21.8 Stream Error States

21.9 Tying an Output Stream to an Input Stream

Chapter 21 - C++ Stream Input/Output Stream

Manipulators

21.6 Stream Manipulators

  • Stream manipulator capabilities
    • Setting field widths
    • Setting precisions
    • Setting and unsetting format flags
    • Setting the fill character in fields
    • Flushing streams
    • Inserting a newline in the output stream and flushing the stream
    • Inserting a null character in the output stream and skipping

whitespace in the input stream

1. Load header

1.1 Initialize variables

2. Input number

3. Output in hex

3.1 Output in octal

3.2 Output in decimal

1 // Fig. 21.16: fig21_16.cpp 2 // Using hex, oct, dec and setbase stream manipulators. 3 #include 4 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 9 #include

10

11 using std::hex;

12 using std::dec;

13 using std::oct;

14 using std::setbase;

15

16 int main()

17 {

18 int n;

19

20 cout << "Enter a decimal number: ";

21 cin >> n;

22

23 cout << n << " in hexadecimal is: "

24 << hex << n << '\n'

25 << dec << n << " in octal is: "

26 << oct << n << '\n'

27 << setbase( 10 ) << n << " in decimal is: "

28 << n << endl;

29

30 return 0;

31 }

Docsity.com

Program Output

  • Enter a decimal number:
  • 20 in hexadecimal is:
  • 20 in octal is:
  • 20 in decimal is:

21.6.3 Field Width (setw,width)

  • ios width member function
    • Sets field width (number of character positions a value should be

output or number of characters that should be input)

  • Returns previous width
  • If values processed are smaller than width, fill characters inserted

as padding

  • Values are not truncated - full number printed
  • cin.width(5);
  • setw stream manipulator

cin >> setw(5) >> string;

  • Remember to reserve one space for the null

character

1. Initialize variables

2. Input sentence

2.1 Set width

2.2 Loop and change

width

3. Output

1 // fig21_18.cpp

2 // Demonstrating the width member function

3 #include

4

5 using std::cout;

6 using std::cin;

7 using std::endl;

8

9 int main()

10 {

11 int w = 4;

12 char string[ 10 ];

13

14 cout << "Enter a sentence:\n";

15 cin.width( 5 );

16

17 while ( cin >> string ) {

18 cout.width( w++ );

19 cout << string << endl;

20 cin.width( 5 );

21 }

22

23 return 0;

24 } Docsity.com

21.6.4 User-Defined Manipulators

  • We can create our own stream manipulators
    • bell
    • ret (carriage return)
    • tab
    • endLine
  • Parameterized stream manipulators
    • Consult installation manuals

21.7 Stream Format States

  • Format flags
    • Specify formatting to be performed during stream I/O operations
  • setf, unsetf and flags
    • Member functions that control the flag settings

21.7.2 Trailing Zeros and Decimal Points

( ios::showpoint )

  • ios::showpoint
    • Forces a float with an integer value to be printed with its decimal

point and trailing zeros

cout.setf(ios::showpoint)

cout << 79;

79 will print as 79.

  • Number of zeros determined by precision settings

21.7.3 Justification ( ios::left, ios::right,

ios::internal )

  • ios::left
    • Fields to left-justified with padding characters to the right
  • ios::right
    • Default setting
    • Fields right-justified with padding characters to the left
  • Character used for padding set by
    • fill member function
    • setfill parameterized stream manipulator
    • Default character is space

1. Initialize variable

2. Use parameterized

stream manipulators

3. Output

1 // Fig. 21.22: fig21_22.cpp 2 // Left-justification and right-justification. 3 #include 4 5 using std::cout; 6 using std::endl; 7 8 #include 9

10 using std::ios;

11 using std::setw;

12 using std::setiosflags;

13 using std::resetiosflags;

14

15 int main() 16 {

17 int x = 12345;

18

19 cout << "Default is right justified:\n"

20 << setw(10) << x << "\n\nUSING MEMBER FUNCTIONS"

21 << "\nUse setf to set ios::left:\n" << setw(10);

22

23 cout.setf( ios::left, ios::adjustfield ); 24 cout << x << "\nUse unsetf to restore default:\n";

25 cout.unsetf( ios::left );

26 cout << setw( 10 ) << x

27 << "\n\nUSING PARAMETERIZED STREAM MANIPULATORS"

28 << "\nUse setiosflags to set ios::left:\n"

29 << setw( 10 ) << setiosflags( ios::left ) << x

30 << "\nUse resetiosflags to restore default:\n"

31 << setw( 10 ) << resetiosflags( ios::left )

32 << x << endl; 33 return 0;

34 }

Docsity.com

Program Output

Default is right justified: 12345

USING MEMBER FUNCTIONS

Use setf to set ios::left: 12345 Use unsetf to restore default: 12345

USING PARAMETERIZED STREAM MANIPULATORS

Use setiosflags to set ios::left: 12345 Use resetiosflags to restore default: 12345

1. Load header

1.1 Initialize variable

1 // Fig. 21.24: fig21_24.cpp

2 // Using the fill member function and the setfill

3 // manipulator to change the padding character for

4 // fields larger than the values being printed.

5 #include

6

7 using std::cout;

8 using std::endl;

9

10 #include

11

12 using std::ios;

13 using std::setw;

14 using std::hex;

15 using std::dec;

16 using std::setfill;

17

18 int main()

19 {

20 int x = 10000; Docsity.com

2. Set fill character

3. Output

Program Output

22 cout << x << " printed as int right and left justified\n"

23 << "and as hex with internal justification.\n"

24 << "Using the default pad character (space):\n";

25 cout.setf( ios::showbase );

26 cout << setw( 10 ) << x << '\n';

27 cout.setf( ios::left, ios::adjustfield );

28 cout << setw( 10 ) << x << '\n';

29 cout.setf( ios::internal, ios::adjustfield );

30 cout << setw( 10 ) << hex << x;

31

32 cout << "\n\nUsing various padding characters:\n";

33 cout.setf( ios::right, ios::adjustfield );

34 cout.fill( '*' );

35 cout << setw( 10 ) << dec << x << '\n';

36 cout.setf( ios::left, ios::adjustfield );

37 cout << setw( 10 ) << setfill( '%' ) << x << '\n';

38 cout.setf( ios::internal, ios::adjustfield );

39 cout << setw( 10 ) << setfill( '^' ) << hex << x << endl;

40 return 0;

41 }

10000 printed as int right and left justified and as hex with internal justification. Using the default pad character (space): 10000 10000 0x 2710

Using various padding characters:


10000%%%%%

0x^^^^2710 Docsity.com