70 likes | 183 Views
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
E N D
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 scientificnotation or problems with fixed formats. • showpoint - ensures that decimal points and trailing zeros are displayed • left – left adjusts output • right - right adjusts output
Examples: • cout.setf(ios::fixed); • cout.setf(ios::showpoint); • cout.setf(ios::fixed | ios::showpoint); • cout.unsetf(ios::right); Note: Important to unset flag!
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
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)
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.