cout formatted output

Posted by cac_azure03 on Thu, 18 Nov 2021 03:49:59 +0100

    Sometimes, we want the output content to be output in the format we specify.
    For example, when outputting a floating-point number, we want to keep two digits after the decimal point; When outputting an integer, you want it to be 8 numbers
When the width is insufficient, fill 0 on the left, and so on.
    At this time, we need to use cout's flow operator (you can also call it format controller)
Flow manipulation operator

Code example

     When writing cout's setiosflags operator, we need to introduce iomanip and iostream header files, and know more about header files
Knowledge, we will explain it in detail later. Here, we only need to understand.
  Add the following code at the top of the file as shown below.

1. Specify base
 
cout << dec << 12 << "," << 24<<endl;
//Use decimal output 12,24
//Output results: 12,24 
cout << hex << 12 << "," << 24<<endl;
//Use hexadecimal output 12,24
//Output result: c,18
cout << oct << 12 << "," << 24<<endl;
//Use octal output 12,24
//Output result: 14,30
cout << setbase(16) << 12 << "," << 24<<endl;
//Same as cout < < hex < < 12 < < "," < 24;
//Use hexadecimal output 12,24
//Output result: c,18
2. Decimal output
cout << fixed << 12.12345678 << endl;
//Output in the form of ordinary decimal. Six decimal places are reserved by default
//Output result: 12.123457
cout << scientific << 12.12345678 << endl;
//Use scientific notation to output decimals
//Output result: 1.212346e+01
3. Sets the specified width and fill
cout << setw(12) << 100 << endl;
//Set the output width to 12 characters, and fill the insufficient space with blank space
//Output result: 100
cout << setw(12) << setfill('0') << 100 << endl;
//Set the output width to 12 characters, and fill the insufficient places with 0
//Output result: 000000000 1000
Note: each setw is only valid for one number. If you want to change the subsequent continuous output width, you need to set it again. example
For example:
cout << setw(8) << 12 << "," <<setw(8)<< 24<<endl;
//Output results: 12, 24
4. Sets the number of significant digits of a number
We can use the setprecision instruction to set the number of digits reserved for the current number. Note that this is rounded
Retention by, for example:
cout << setprecision(4) << 3.1415926<<endl;
//Keep 4 significant digits
//Output result: 3.142
setprecision, together with the fixed command, can retain the decimal number of specified digits, for example:
cout << fixed << setprecision(4) << 3.1415926 << endl;
//Keep 4 decimal places
//Output result: 3.141
setprecision, together with the scientific command, can realize the scientific notation of retaining the specified digits, for example:
cout << scientific << setprecision(2) << 3.1415926 << endl;
//Keep 2 decimal places and output using scientific notation
//Output result: 3.14e+00
5. Align left and right
cout << setw(12) << right << 12.1 << endl;
//Set the output width to 12 characters to keep the output data right aligned
//Output result: 12.1
cout << setw(12) << left << 12.1 << endl;
//Set the output width to 12 characters to keep the output data aligned to the left
//Output result: 12.1
6. Line feed
There are two commonly used line breaks: endl and \ n, as shown below:
cout << "We are" << endl;
cout << "in different\n";
cout << "line";
//Output results:
//We are
//in different
//line
7. Hyphen
Sometimes we don't want to write so many cout instructions, but we don't want to display them on the same line. What's a good way? this
The hyphen "\" is required for the following example:
cout << "We are in\
the same line";
 //Use connectors to connect two lines of content
 //Output result: We are in the same line
Exercises
1. Align right in one line and output 3, 4 and 5. Each number accounts for 8 characters in width, and the numbers are separated by spaces.
2. Output 11.87243432, with 4 significant digits reserved.
3. Output 11.87243432, with 4 decimal places reserved.
4. Use scientific notation to output 11.87243432.
5. The letters a, B and C are output in three lines. Two different line breaks are required.
6. Output left aligned 123 and right aligned 321 in two lines, accounting for 12 characters each.
7. Use 8, 10 and hexadecimal to output 10 respectively, each occupying one line.
8. 10100100 is output in three lines, each accounting for 12 characters, and 0 is used to fill the empty space.
9. Use hyphens to output hello world on one line.
Reference code
1,
cout << setw(8) << 3 << " " << setw(8) << 4 << " " << setw(8)
<< 5 << " " << endl;

2,

cout << setprecision(4) << 11.87243432;

3,

cout << fixed << setprecision(4) << 11.87243432;

4,

cout << scientific << 11.87243432;

5,

cout << "a" << endl;
cout << "b\n";
cout << "c";

6,

cout << left << setw(12) << 123 << endl;
cout << right << setw(12) << 321 << endl;

7,

cout << oct << 10 << endl;
cout << dec << 10 << endl;
cout << hex << 10 << endl;

8,

cout << setw(12) << setfill('0') << 10 << endl;
cout << setw(12) << setfill('0') << 100 << endl;
cout << setw(12) << setfill('0') << 1000 << endl;

9,

cout << "Hello \
world";
Logic route training and excellent education, Informatics Olympiad training expert.
Scan the code to add the author for more content.

Topics: C++