Use of cout format output

Source: Internet
Author: User

A controller is an object defined in iomanip. h In the header file. Iomanip. h must be included before use

1. I/O writing format

The I/0 stream is a series of input or output bytes. When the program needs to display the output on the screen, you can use the insert operator "<" to insert characters into the cout output stream. For example:
Cout <"this is a program./N ";
When the program needs to perform keyboard input, you can use the extraction operator ">" to extract characters from the CIN abortion. For example:
Int myage;
Cin> myage;
No matter what basic data type name or value is sent to the stream, it can understand.
For example, the following function outputs strings and integers:
# Include <iostream. h>
Void main ()
{
Cout <"My name is Jone/N ";
Cout <"the ID is ";
Cout <2;
Cout <Endl;
}
The above output can also be serialized in the same line. The following output statement is the same as the above output:
Cout <"My name is Jone/N" <"the ID is" <2 <Endl;
It can also be divided into several lines to improve readability. The following statements output the same result as the previous example:
Cout <"My name is Jone N" // No semicolon at the end of the row
<"The ID is"
<2
<Endl;
Cin can adjust rows in the same way as cout, which automatically recognizes the location and type of variables. For example:
Int I; float F; long l;
Cin> I> F> L;
Cin can know the type of the extracted variable. It will give an integer, floating point, and long integer to I, f, and L respectively.

2. Use a controller

The default stream format output sometimes does not meet special requirements, such:

Double average = 9.400067;
Cout <average <Endl;

The expected value is 9.40, that is, the two decimal places are retained, but 9.40007 is displayed. The default value is 6 Valid digits. Manipulators can be used to control the format of an I/O Stream. A controller is an object defined in iomanip. h In the header file. You can directly Insert the control operator into the stream. Common controllers are listed in Table 2-4.

Table 2-4 common controllers for I/O streams

Control operator
Description

Dec
HEX
Oct
Setfill (c)
Setprecision (N)
SETW (N)
Setiosflags (IOs: fixed) setiosflags (IOs: Scientific) setiosflags (IOs: Left) setiosflags (IOs: Right) setiosflags (IOs: skipws) setiosflags (IOs :: uppercase) setiosflags (IOs: lowercase)
Set base number to 10
Set base number to 16
Set base number to 8
The character C is not filled
The decimal precision is n digits.
Set the domain width to n characters
Fixed floating point display
Exponential Representation
Left aligned
Right alignment
Ignore leading Blank
Hexadecimal number uppercase output
Hexadecimal lowercase output
 

 

When using a controller, add the header file iomanip. H to the program header.

3. Control floating point value display

You can use setprecision (n) to control the number of floating point numbers displayed in the output stream. The default value of C ++ is 6.
If setprecision (n) is used with setiosflags (IOs: fixed), you can control the number of digits on the right of the decimal point. Setiosflags (IOs: fixed) is a real number expressed by a fixed point.
If used with setiosnags (IOs: Scientific), you can control the number of decimal places in exponential notation. Setiosflags (IOs: Scientific) is an exponential representation of real numbers.
For example, the following code represents a real number by floating point, fixed point, and exponent respectively:

//*********************
// ** Ch2_1.cpp **
//*********************

# Include <iostream. h>
# Include <iomanip. h> // format controller is required

Void main ()
{
Double amount = 22.0/7;
Cout <amount <Endl;
Cout <setprecision (0) <amount <Endl
<Setprecision (1) <amount <Endl
<Setprecision (2) <amount <Endl
<Setprecision (3) <amount <Endl
<Setprecision (4) <amount <Endl;

Cout <setiosflags (IOs: fixed );
Cout <setprecision (8) <amount <Endl;

Cout <setiosflags (IOs: Scientific)

<Amount <Endl;

Cout <setprecision (6); // reset to the original default setting
}

The running result is:
3.14286
3
3
3.1
3.14
3.143
3.14285714
3.14285714e + 00

The program runs on a 32-bit machine.
In the output represented by floating point, setprecision (n) indicates the number of valid digits.
The number of valid digits is not set before the output value of the 1st row. Therefore, the default value of the valid digits of the stream is 6: 0 for the 2nd output, and the minimum valid digits of C ++ are 1, as a result, set the number of valid digits to 1: 3rd ~ 6 rows of output are output according to the specified number of valid digits.
In the output represented by a fixed point, setprecision (n) indicates the number of decimal places.
7th row output is used with setiosflags (IOs: fixed. Therefore, setprecision (8) sets the number of digits after the decimal point, not the total number of digits.
When output in exponential form, setprecision (n) indicates the number of decimal places.
8th row output uses setiosflags (IOs: Scientific) to represent the output form of exponential representation. The valid BITs follows the last set value of 8.
When the number of decimal places is truncated, 4 homes and 5 orders are processed.

4. Set the output width of the value.

In addition to space to forcibly control the output interval, you can also use the SETW (n) controller. If a value requires more characters than the number of characters determined by SETW (n), the value uses all the characters it requires. For example:
Float amount = 3.14159;
Cout <SETW (4) <amount <Endl;
The result is 3.14159. It is output according to the actual width instead of the four-bit width.
If the number of characters in a value is less than the number of characters determined by SETW (N), a blank space is displayed before the number. Unlike other controllers, SETW (n) only affects the output of the next value, in other words, the interval set by SETW does not retain its effect. For example:
Cout <SETW (8)
<10
<20 <Endl;
The running result is:
------- 1020
The underline in the running result indicates space. The integer 20 is not output by 8 in width. The default value of SETW () is 0 in width, that is, SETW (0), which means the output width is represented by the output value, so 20 is next to 10. If you want each value to have a width of 8, you must set each value:
Cout <SETW (8) <10
<SETW (8) <20 <Endl;

5. Output octal and hexadecimal numbers

The three commonly used controllers are hex, Oct, and Dec. They correspond to the display of hexadecimal, octal, and 10hexadecimal numbers respectively. The three controllers are defined in the iostream. h header file. For example:
//*********************
// ** Ch2_2.cpp **
//*********************

# Include <iostream. h>

Void main ()
{
Int number = 1001;
Cout <"decimal:" <dec <number <Endl
<"Hexadecimal:" <"Octal:" <Oct <number <Endl;
}

The running result is:
Decimal: 1001
Hexadecimal: 3e9
Octal: 1751
1001 is a decimal number. It cannot be interpreted as a hexadecimal number or an octal number because it does not start with 0x or 0. However, in the output, the stream is filtered based on the control operator so that it is displayed in a certain order.
You can use the setiosflags (IOs: uppercase) in the header file iomanip. h to control hexadecimal output in uppercase. For example, add a header file in the above example and control the hexadecimal number in upper case, that is:
# Include <iostream. h>
# Include <iomanip. h>
//...

Cout <"hexadecimal:" <Setiosftags (IOs: uppercase)
<Number <Endl;
The hexadecimal value is hexadecimal: 3e9.

6. Set Filling Characters

SETW can be used to determine the display width. By default, the stream uses a space character to ensure the correct interval between characters. You can use the setfill control to determine a non-space character. Setfill is defined in the header file iomanip · h. For example:
//*********************
// ** Ch2_3.cpp **
//*********************

# Include <iostream. h>
# Include <iomanip. h>

Void main ()
{
Cout <setfill (''*'')
<SETW (2) <21 <Endl
<SETW (3) <21 <End
<SETW (4) <21 <Endl;

Cout <setfill ('''); // restore the default setting
}

The running result is:
21
* 21
** 21

7. Align left and right output

By default, the content displayed on the left alignment of the I/O Stream. Use the setiosflags (IOs: left) and (IOs: Right) signs in the header file iomanip. h to control output alignment. For example:
//*********************
// ** Ch2_4.cpp **
//*********************

# Include <iostream. h>
# Include <iomanip. h>

Void main ()
{
Cout <setiosflags (IOs: Right)
<SETW (5) <1
<SETW (5) <2

<SETW (5) <3 <Endl;

Cout <setiosflags (IOs: left)
<SETW (5) <1
<SETW (5) <2
<SETW (5) <3 <Endl;
}

The running result is:
----- 1-----2-----3
1-----2-----3 -----

8. Force display of decimal points and symbols

When the program outputs the following code:
Cout <10.0/5 <Endl;
The default I/0 stream will simply display 2 instead of 2.0, because the division result is accurate. To display the decimal point, you can use the ISO: showpoint flag. For example:
//*********************
// ** Ch2_5.cpp **
//*********************

# Include <iostream. h>
# Include <iomanip. h>

Void main ()
{
Cout <10.0/5 <Endl;

Cout <setiosflags (IOs: showpoint)
<10.0/5 <Endl;
}

The running result is:
2
2.00000
By default, the I/O Stream only displays the value symbol before the negative number. According to the purpose of the program, you sometimes need to add a positive number before the positive number. You can use the IOS: showpos flag. For example:
//*********************
// ** Ch2_6.cpp **
//*********************

# Include <iostream. h>
# Include <iomanip. h>

Void main ()
{
Cout <10 <"" <-20 <Endl;

Cout <setiosflags (IOs: showpos)
<10 <"" <-20 <Endl;
}

The running result is:
10-20
+ 10-20

Summary
 
A variable is the name assigned by the program to a memory location. It can store information. Before using a variable, the program must first describe the variable name and variable type.
Different variables cannot have the same name. The variable name should reflect the purpose of the variable as much as possible to enhance the readability of the program.
During the program running, the constant value cannot be changed. Constants also have various data types and occupy storage space. The data representation of various data types has a certain range. Beyond this range, C ++ needs to intercept the data so that the data is no longer correct.
Cout can be used to output data of various data types and display output information (including special symbols) on the screen in multiple ways ).
C ++ is compatible with C library functions, so printf () and scanf () can also be used as usual.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.