Presentation is loading. Please wait.

Presentation is loading. Please wait.

Formatting the Output The C++ standard library supplies many manipulators: endl, setw, fixed, showpoint, setprecesion. If we want to use endl, fixed, or.

Similar presentations


Presentation on theme: "Formatting the Output The C++ standard library supplies many manipulators: endl, setw, fixed, showpoint, setprecesion. If we want to use endl, fixed, or."— Presentation transcript:

1 Formatting the Output The C++ standard library supplies many manipulators: endl, setw, fixed, showpoint, setprecesion. If we want to use endl, fixed, or showpoint, we need to include the header file iostream. #include <iostream> If we want to use setw or setprecision, we need to include the header file iomanip. #include <iomanip>

2 The manipulator setw Setw means set width.
The argument of setw is an integer. This integer gives the field width. The output is right-justified. Example: int ans = 33; int num = 7132; Output cout << setw(4) << ans  33 7132  Hi << setw(5) << num << set(4) <, “Hi”; Cout << setw(1) << ans 7132 << setw(5) << num; 4 5 4 5 Field width will expand to fit the 2-digit value

3 The manipulator setprecision
Value of x Statement Output cout << setw(10) << setprecision(2) << x;    310.00 << setprecision(5) << x;  cout << setw(7) << setprecision(5) << x; (expands width to 9) cout << setw(6) << setprecision(2) << x;  4.83 (last displayed digit is round off)

4 Manipulators fixed and showpoint
You can use manipulator named fixed to force all subsequent floating point output to appear in decimal form rather than scientific notation: cout << fixed << 3.8 * x; To force decimal points to be displayed in subsequent floating-point output, even for whole numbers, you can use the manipulator showpoint: cout << showpoint << floatVar;


Download ppt "Formatting the Output The C++ standard library supplies many manipulators: endl, setw, fixed, showpoint, setprecesion. If we want to use endl, fixed, or."

Similar presentations


Ads by Google