40 likes | 178 Views
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
E N D
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>
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 33 7132 << setw(5) << num; 4 5 4 5 Field width will expand to fit the 2-digit value
The manipulator setprecision Value of x StatementOutput 310.0 cout << setw(10) << setprecision(2) << x; 310.00 310.0 cout << setw(10) << setprecision(5) << x; 310.00000 310.0 cout << setw(7) << setprecision(5) << x; 310.00000 (expands width to 9) 4.827 cout << setw(6) << setprecision(2) << x; 4.83 (last displayed digit is round off)
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;