In the C language, it is common to use formatted input and output functions printf and scanf for entering or outputting data or information. In the C + + language, we can still use this set of input and output libraries, but the C + + language also customizes a new, easier-to-use input and output library.
In a C + + program, input and output can be thought of as a series of streams of data, which can be considered as a stream of data from a file or keyboard, and the output may be considered as outputting a stream of data from the program to the display or file.
Connotation of the Satin update
When you write a C + + program, you need to include the header file iostream if you need to use the input output . objects for input and output are defined in iostream, such as common cin for standard input, cout for standard output, and cerr for standard errors.
Youku Account Notifications
It is important to emphasize that CIN, cout, cerr are not keywords in C + +, they are essentially function calls, and their implementations are using C + + operator overloading, which will be described later. The output destinations of both cout and Cerr are displays, but the difference is that the cout is buffered, and cerr is not buffered.
When we use the cout for output, we need to use the "<<" operator immediately, and we need to use the ">>" operator when using CIN for input, which can analyze the type of data processed by itself. So we don't need to set the input-output format statement like we did with scanf and printf.
[Example 1] Examples of simple input and output code for C + +:
The function of this program is to prompt the user to enter an integer and a floating-point number, and then display it on the display, and this code will run as follows (indicating that the user pressed Enter):
Please input an int number:
8
The int number is x= 8
Please input a float number:
7.4
The float number is y= 7.4
The statement cout<< "please input a int number:" <<endl; represents the output "" "". "" ""} Input a int number: "Such a hint of discourse, let the user know that input an integer data, where Endl represents a newline output, and its function is the same as the C language "\ n", in this program we can also use "\ n" to replace the Endl. It is important to note that the last letter of Endl is the letter "L", not the Arabic numeral "1", whose English is all called "End of Line". The statement cin>>x; indicates that data from the standard input is read into an int type and deposited into the X variable. If the user enters data that is not of type int at this time, it is coerced into int type data. The statement cout<< "the int number is x=" <<x<<endl; will be the input integer data output, from which we can see that cout can be continuously output. Likewise CIN is also supported on multiple variables
Music View Members Share
Continuous input, as shown below.
[Example 2]cin continuous Input Sample:
Youku Share Account
Operation Result:
Please input a int number and a float number:
8 7.4
The int number is x= 8
The float number is y= 7.4
In Example 2 we use cin>>x>>y; to continuously read an integer and a floating-point number from the standard input, and then deposit them into X and Y, respectively. Input operator >> The space after the previous item is ignored before the next entry is read, as in Example 2, there is a space between the number 8 and 7.4, and when Cin reads 8, it ignores the space and then reads 7.4.
July 7 Thunderbolt account sharing
When using the Cin>>val input variable, if you enter a variable into Val, the expression returns true, otherwise false is returned. With this feature, we can enter data continuously, as shown in Example 3.
[Example 3] continuous input data using CIN:
yuan7897891 User Information
The program continuously reads data from the standard input and stores it in a Val variable, and every time a data is read into it, the program will output the sum of all the data before the user enters the file Terminator identifier.
C + + input and output