1 / 4

Formatting the Output

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

elinor
Download Presentation

Formatting the 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. 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 33 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 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)

  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;

More Related