IOS development-C ++ Input and Output

Source: Internet
Author: User

Recommended by blue ou iOS Training Instructors: This section will continue to introduce C ++ to you. The specific content is as follows:

In C ++, input and output are not formal components. C and C ++ do not provide a special statement structure for input and output, the input/output is not defined by C ++ itself, but is defined in the I/O library provided by the compilation system.

C ++ outputs and inputs are implemented in the form of "stream", indicating the process of C ++ inputting and outputting through the stream, such:

650) this. width = 650; "title =" c00000000by streaming input and output process .jpg "src =" http://www.bkjia.com/uploads/allimg/131229/1212064637-0.jpg "alt =" 161097413.jpg"/>

650) this. width = 650; "title =" c00000000by inbound input and output process 1.jpg "src =" http://www.bkjia.com/uploads/allimg/131229/1212063556-1.jpg "width =" 418 "height =" 45 "alt =" 161917907.jpg"/>

The definitions of stream objects such as cin, cout, and stream operators are stored in the input and output stream library of C ++. Therefore, if you use the cin, cout, and stream operators in a program, you must use the preprocessing command to include the header file stream in this file:

# Include <iostream>

Although cin and cout are not statements provided by C ++ itself, in the case of no confusion, we often call the input statement ">" by the cin and stream extraction operators as the input statement or the cin statement, the output statement implemented by the cout and stream insertion operator <is called an output statement or a cout statement. According to the C ++ syntax, as long as an operation can be implemented and ended with a semicolon, it is a statement.

I. Basic operations on input and output streams

The common format of cout statements is:

Cout <expression 1 <expression 2 <...... <Expression n;

The general format of the cin statement is:

Cin> variable 1> variable 2 >>……> Variable n;

When defining a stream object, the system opens a buffer in the memory to temporarily store the data of the input and output streams. When executing a cout statement, store the inserted data in the output buffer sequence until the output buffer is full or the endl (or '\ n', ends, flush) in the cout statement is run.

Output the existing data in the buffer zone together and clear the buffer zone. The data in the output stream is output on the system's default device (generally the display.

A cout statement can be written into several lines, for example:

650) this. width = 650; "title =" A cout statement can be divided into several lines, for example:. jpg "src =" http://www.bkjia.com/uploads/allimg/131229/12120C4a-2.jpg "alt =" 161949917.jpg"/>

You must note that you cannot use an insert operator <to insert multiple output items, for example:

Cout <a, B, c; // error. Multiple times cannot be inserted at a time.

Cout <a + B + c; // correct. This is an expression

When cout is used for output, the user does not need to notify the computer of the type of output. The system automatically identifies the type of output data so that the output data is output according to the corresponding type, if a is defined as int type, B is float type, and c is char type, then:

650) this. width = 650; "title =" if a is defined as int type, B is float type, and c is char type, then :. jpg "src =" http://www.bkjia.com/uploads/allimg/131229/1212061Y2-3.jpg "alt =" 162002593.jpg"/>

In the above example, you can enter 1234 on the keyboard

You can also input data in multiple rows:
1
23
4
When using cin to input data, the system also extracts bytes of the corresponding length from the input stream based on the variable type. For example:
Charc1, c2;
Inta;
Floatb;
Cin> c1> c2> a> B;
If you enter
123456.78

Note: There should be spaces behind 34 to separate them from 56.78. You can also enter them in the following format:

123456.78 (there is a space between 1 and 2)

Do not use the cin statement to enter space characters and carriage return linefeeds as character variables. They will be skipped. If you want to use a space character or carriage return linefeed (or any other character on the keyboard) enter the character variable. You can use the getchar function described in section 3.4.3.

When organizing input stream data, you must carefully analyze the type of variables in the cin statement and input them in the corresponding format. Otherwise, errors may occur.

2. Use a controller in the input stream and output stream

From the above introduction, we use the default format for cout and cin, but sometimes people have some special requirements for input and output, such as specifying the field width when outputting a real number and retaining only two decimal places, data is aligned to the left or right. C ++ provides the control operator used in the input/output stream)

Note: If the controller is used, you must add the iostream header file and the iomanip header file at the beginning of the Program unit.

For example, the number of outputs with Double Precision:

650) this. width = 650; "title =" For example: Output Double Precision number .jpg "src =" http://www.bkjia.com/uploads/allimg/131229/12120AP8-4.jpg "width =" 471 "height =" 177 "alt =" 162030712.jpg"/>

Example: integer output:

650) this. width = 650; "title =" Example: integer output .jpg "src =" http://www.bkjia.com/uploads/allimg/131229/12120BC9-5.jpg "width =" 469 "height =" 155 "alt =" 162043371.jpg"/>

If the same setw (n) and setiosflags (ios secure right) are used in multiple cout statements, the right alignment of each row of data can be achieved,

If the same precision is specified, the upper and lower decimal points can be aligned.

For example, each row is aligned with the decimal point.

650) this. width = 650; "title =" For example: the rows of small point to Qi .jpg "src =" http://www.bkjia.com/uploads/allimg/131229/12120B056-6.jpg "width =" 473 "height =" 269 "alt =" 162057803.jpg"/>

From the above example, you can understand the content of this lesson. You can practice it yourself and see the output results.

Related Article

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.