1 / 7

Formatting Screen Input/Output

INFSY 307 C++. Formatting Screen Input/Output. Format Flags setf (ios::<flagname>) unsetf(ios::<a flag>); common flagnames: fixed left showpoint right. Formatting flags. Fixed - ensures fixed format, i.e no

colman
Download Presentation

Formatting Screen Input/Output

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. INFSY 307 C++ Formatting Screen Input/Output

  2. Format Flags • setf (ios::<flagname>) • unsetf(ios::<a flag>); • common flagnames: • fixed • left • showpoint • right

  3. Formatting flags • Fixed - ensures fixed format, i.e no scientificnotation or problems with fixed formats. • showpoint - ensures that decimal points and trailing zeros are displayed • left – left adjusts output • right - right adjusts output

  4. Examples: • cout.setf(ios::fixed); • cout.setf(ios::showpoint); • cout.setf(ios::fixed | ios::showpoint); • cout.unsetf(ios::right); Note: Important to unset flag!

  5. Formatting Manipulator Manipulators are placed after the insertion operator: setprecision (2); - two decimal digits are output setw (4); - describes width of next field - cout.width (4); or cout.precision (2) is an alternative Note: cout << is required #include <iomanip.h> is required

  6. Formatting Flags and Manipulators Examples cout<< setprecision (2)<<“Value is: “<<10.5; cout<<setw(7)<<value1<<setw(5)<<value2<<endl; cout<< setw (4)<<“abc”; (note only for next variable) cout.precision (4); cout.width (10); (note: width is only for next next variable precisision remains set)

  7. Formatting Rules • Flags and most manipulators remain set until reset • When a field is too small, C++ will adjust it and print the • true value of a number • 3. When a string variable is output, the field width will be • filled with whatever is in memory unless a ‘\0’ is found. • ‘\0’ is referred to as the NULL character • and is a special character similar to ‘\n’ • 4. Extra bytes are padded with spaces.

More Related