Presentation is loading. Please wait.

Presentation is loading. Please wait.

Formatting Screen Input/Output

Similar presentations


Presentation on theme: "Formatting Screen Input/Output"— Presentation transcript:

1 Formatting Screen Input/Output
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
scientific notation 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: Note: Important to unset flag! 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.


Download ppt "Formatting Screen Input/Output"

Similar presentations


Ads by Google