• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Formatted I/O in C++ Tutorial by Arvind K.
 

Formatted I/O in C++ Tutorial by Arvind K.

on

  • 602 views

I have compiled an easy-moderate level tutorial on Formatted I/O in C++

I have compiled an easy-moderate level tutorial on Formatted I/O in C++

Statistics

Views

Total Views
602
Views on SlideShare
602
Embed Views
0

Actions

Likes
0
Downloads
5
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Formatted I/O in C++ Tutorial by Arvind K. Formatted I/O in C++ Tutorial by Arvind K. Document Transcript

    • Formatted I/O in C++We can format our output using various stream manipulators presentin <iomanip.h> header filea) Stream Format State Flag  Format state flag specify the kinds of formatting needed during the stream operations.  Available member functions used to control the flag setting are: setf(), unsetf() and flags().  The one argument, setf() function specifies one or more flags.  Format state flags are defined as an enumeration in class ios. The list for some of the flags is shown below: Format state flags Brief description ios::adjustfield Controlling the padding, left, right or internal. ios::left Use left justification. ios::right Use right justification. ios::internal Left justify the sign, right justify the magnitude. ios::basefield Setting the base of the numbers. ios::dec Use base 10, decimal. ios::oct Use base 8, octal. ios::hex Use base 16, hexadecimal. ios::showbase Show base indicator on output. ios::showpoint Shows trailing decimal point and zeroes. ios::uppercase Use uppercase for hexadecimal and scientific notation values. ios::showpos Shows the + sign before positive numbers. ios::floatfield To set the floating point to scientific notation or fixed format. ios::scientific Use scientific notation. ios::fixed Use fixed decimal point for floating-point numbers. Table 1: State flag formatPrepared By : Arvind Kumar Btech 3CSE-1 Amity University Page # 1
    • b) Trailing Zeroes and signs of positive/negative numbers  ios::showpoint – this flag is set to force a floating point number to be output with its decimal point and trailing zeroes.  ios::showpos – this flag is used to show ‘+’ or ‘—’ sign before positive/negative numbers. // Example showing THE Difference and scope of setf() cout << 45.420000 << “n”; cout.setf(ios::showpoint); cout << 45.420000 << “n”; cout << 45.420000 << “n”; cout.unsetf(ios::showpoint); cout << 45.420000 << “n”; cout.setf(ios::showpoint | ios::showpos); cout << 45.420000 << “n”; cout << -45.420000 << “n”; cout.unsetf(ios::showpoint | ios::showpos); Output: 45.42 45.4200 45.4200 45.42 +45.4200 -45.4200Prepared By : Arvind Kumar Btech 3CSE-1 Amity University Page # 2
    • b) Floating point precision  setprecision() used with ios::scientific or ios::fixed flag, sets max. number of digits which can be printed after decimal point.  setprecision() if used without ios::scientific or ios::fixed flag, sets total no. of digits to print including numbers to the left of decimal and also to the right of decimal point.  setprecision() is sticky, it sticks to subsequent cout statements. Example for using setprecision() double a = 88.466666666f ; cout << setprecision(1) << a << "n" ; cout << setprecision(2) << a << "n" ; cout << setprecision(4) << a << "n" ; cout << setprecision(8) << a << "n" ; cout << a << "n" ; cout.setf(ios::scientific); cout << setprecision(1) << a << “n” ; cout << setprecision(2) << a << “n” ; cout.unsetf(ios::scientific); Output : 9e+01 88 88.47 88.466667 88.466667 8.8e+01 8.847e+01Prepared By : Arvind Kumar Btech 3CSE-1 Amity University Page # 3
    • c) Field width and padding  Sets the field width. If values processed are smaller than the field width, fill characters are inserted as padding. Wider values will not be truncated.  Use setw() to set field width  Use setfill() to set a ‘fill character’ to fill empty spaces of field.  Syntax : setw(int); setfill(‘char’);  Note : setw() has scope of only one cout statement only. setfill() is sticky and comes in subsequent cout statements also. setfill() takes only a character as an argument , always include ‘ ’ Example : cout << setw(6) << 51 << "n"; cout << setw(6) << setfill(*) << 51 << "n"; cout << setw(6) << 51 << "n"; cout << setw(6) << setfill( ) << 51 << "n"; Output : ****51 ****51 ****51 ****51Prepared By : Arvind Kumar Btech 3CSE-1 Amity University Page # 4
    • d) Scientific Notations  ios::scientific – this flag formats floating-point numbers in scientific notation. -- In scientific notation 858.50 = 8.5850 x 102 or 8.585e2  ios::fixed – this flag formats floating-point numbers in fixed notation. -- In fixed notation, 88.55 x 104 or 88.55e4 = 885500 Example : double m = 88.55e4; double n = 858.50; cout.setf(ios::scientific); cout << setprecision(2) << m << "n"; cout << n << "n"; cout.setf(ios::fixed); cout << m << "n"; cout << n << "n"; Output : 8.86e+05 8.58e+02 885500.00 858.50Prepared By : Arvind Kumar Btech 3CSE-1 Amity University Page # 5
    • e) Base field  ios::basefield – includes the hex, oct and dec bits to specify that integers are to be treated as hexadecimal, octal and decimal values respectively  If none of these bits is set, stream insertions default to decimal. Integers starting with 0 are treated as octal values, starting with 0x or 0X are treated as hexadecimal values and all other integers are treated as decimal values. So, set the showbase if you want to force the base of values to be output.  Note : hex, oct, dec are sticky and effect subsequent cout statements  Use ios::showbase to show numbers in respective notations of their baseExample int k = 15; cout << k << "n"; cout << hex << k << "n"; cout << k << "n"; cout << oct << k << "nn"; cout.setf(ios::showbase); cout << dec << k << "n"; cout << hex << k << "n"; cout << k << "n"; cout << oct << k << "nn"; Output : 15 f f 17 15 0xf 0xf 017Prepared By : Arvind Kumar Btech 3CSE-1 Amity University Page # 6
    • f) Justification  Use for left, right or internal justification.  ios::left – enables fields to be left-justified with padding characters to the right.  ios::right – enables fields to be right-justified with padding characters to the left.  The character to be used for padding is specified by the fill or setfill.  ios::internal – this flag indicates that a number’s sign (or base if ios::showbase flag is set) should be left-justified within a field, the number’s magnitude should be right-justified and the intervening spaces should be padded with the fill character.  The left, right and internal flags are contained in static data member ios::adjustfield, so ios::adjustfield argument must be provided as the second argument to setf when setting the right, left or internal justification flags because left, right and internal are mutually exclusive. Example : cout.setf(ios::showpos); cout.setf(ios::left); cout << setw(6) << 66 <<"n"; cout.setf(ios::right); cout << setw(6) << 66 <<"n"; cout.setf(ios::internal); cout << setw(6) << 66 <<"nn"; cout.unsetf(ios::adjustfield | ios::showpos); Output : +66 +66 + 66Prepared By : Arvind Kumar Btech 3CSE-1 Amity University Page # 7