C + + input cin and output cout

Source: Internet
Author: User

C + + input cout and output cin input and output are not formal components of the C + + language. Neither C nor C + + itself provides a specialized statement structure for inputs and outputs. The input output is not defined by the C + + itself, but is defined in the I/O library provided by the compilation system.

The output and input of C + + is implemented in the same way as stream. Figure 3.2 and Figure 3.3 represent the process of C + + input and output through a stream. Information about the definition of the Stream object cin, cout, and stream operators is stored in the input and output stream Library of C + +, so if you use the CIN, cout, and stream operators in your program, you must use the preprocessing command to include the head file stream in this file:
#include <iostream>
Although Cin and cout are not statements provided by C + + itself, they are often referred to as input statements or CIN statements by the cout and stream insertion operator "<<" in order to facilitate the narrative without confusion. The statement that implements the output is called an output statement or a cout statement. According to the C + + syntax, there are statements that can achieve some kind of operation and end with a semicolon.
Basic operation of the input stream and output stream the general format of the cout statement 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 creates a buffer in memory that is used to stage data from the input and output stream. When executing the cout statement, the inserted data order is stored in the output buffer until the output buffer is full or the Endl (or ' \ n ', Ends,flush) in the cout statement is encountered, the data that is already in the buffer is output, and the buffer is emptied. The data in the output stream is output from the system's default device (typically the monitor).

A cout statement can be divided into several lines. Such as
cout<< "This was a simple C + + program." <<endl;
can be written
cout<< "This is"//note line end no semicolon
<< "A C + +"
<< "program."
<<endl; The end of the statement has a semicolon
Can also be written as multiple cout statements, i.e.
cout<< "This is"; A semicolon at the end of the statement
cout << "A C + +";
cout << "program.";
cout<<endl;
The output of the above 3 cases is
This was a simple C + + program.

Note that you cannot insert multiple output items with an insert operator "<<", such as:
cout<<a,b,c; Error, cannot insert multiple items at one time
cout<<a+b+c; Right, this is an expression, as a

When using the cout output, the user does not have to inform the computer what type of output, the system will automatically determine the type of output data, so that the output of data according to the corresponding type output. If a is defined as int, B is float and C is char, then
cout<<a<< ' <<b<< ' <<c<<endl;
is output in the following form:
4 345.789 A

Similar to cout, a CIN statement can be divided into several lines. Such as
cin>>a>>b>>c>>d;
can be written
Cin>>a//Note line end no semicolon
&GT;&GT;B//This may seem clearer
>>c
>>d;
can also be written
cin>>a;
cin>>b;
cin>>c;
cin>>d;
All 3 of the above can be entered from the keyboard:
1 2 3 4

You can also enter data in multiple rows:
1
2 3
4

When you enter with CIN, the system also extracts the corresponding length of bytes from the input stream based on the type of the variable. If there is
Char C1, C2;
int A;
float B;
cin>>c1>>c2>>a>>b;
If you enter
1234 56.78

Note: There should be a space after 34 to be separated from the 56.78 points. You can also enter it in the following format:
1 2 34 56.78 (there are spaces between 1 and 2)

You cannot use the CIN statement to enter a space character and a carriage return newline character as characters into a character variable, and they will be skipped. You can use the GetChar function if you want to enter a space character or carriage return (or any character on any other keyboard) to a character variable.

In the organization input stream data, to carefully analyze the type of the variables in the CIN statement, input according to the appropriate format, otherwise error-prone.
The use of the control in the input stream and output stream is described above in the default format when using cout and CIN. But sometimes people have special requirements for input and output, such as specify the field width when outputting real numbers, keep only two decimal places, align data to the left or right, and so on. C + + provides the controls used in the input and output stream (some are called manipulators), as shown in table 3.1.

Table 3.1 controls for input and output streams
Control character function
Dec Set the cardinality of the numeric value to 10
Hex Set the cardinality of the numeric value to 16
Oct Set the cardinality of the numeric value to 8
Setfill (c) Set the fill character c,c can be a character constant or a character variable
Setprecision (N) Sets the precision of the floating-point number to n bits. When output is in normal decimal decimal form, n represents a valid number. n is the number of decimal digits when output is in the form of fixed decimal digits and scientific (exponential)
SETW (N) Set field width to n bits
Setiosflags (ios::fixed) Set a floating point number to display in a fixed number of decimal digits
Setiosftags (ios::scientific) Set floating-point numbers to display in scientific notation (i.e. exponential form)
Setiosflags (Ios::left) Left alignment of output data
Setiosflags (Ios::right) Right alignment of output data
Setiosflags (IOS::SKIPWS) Ignore leading spaces
Setiosflags (Ios::uppercase) When the data is output in 16 binary form, the letters are capitalized
Setiosflags (Ios::lowercase) When the data is output in 16 binary form, the Yu mother is represented in lowercase
Setiosflags (Ios::showpos) The "+" number is given when the output is positive

Note: If you use a control, the beginning of the program unit in addition to add iostream header file, but also add Iomanip header file.

For example, the output double precision number:
Double a=123.456789012345; Assign an initial value to a

1) cout<<a; Output: 123.456
2) cout<<setprecision (9) <<a; Output: 123.456789
3) cout<<setprecision (6); Restore the default format (6 accuracy)
4) cout<< setiosflags (ios∷fixed); Output: 123.456789
5) Cout<<setiosflags (ios∷fixed) <<setprecision (8) <<a; Output: 123.45678901
6) Cout<<setiosflags (ios∷scientific) <<a; Output: 1.234568e+02
7) cout<<setiosflags (ios∷scientific) <<setprecision (4) <<a; Output: 1.2346E02

The following is an example of an integer output:
int b=123456; Initial value of B assignment
1) cout<<b; Output: 123456
2) cout<3) cout<<setiosflags (ios∷uppercase) <<b; Output: 1E240
4) COUT&LT;&LT;SETW (Ten) <<b<< ', ' <<b; Output: 123456,123456
5) Cout<<setfill (' * ') &LT;&LT;SETW (Ten) <<b; Output: * * * 123456
6) Cout<<setiosflags (Ios∷showpos) <<b; Output: +123456

If you use the same SETW (n) in multiple cout statements and use Setiosflags (ios::right), you can achieve right alignment of each row of data, and if you specify the same precision, you can align the upper and lower decimal points.

"Example 3.1" each line is aligned to the decimal point.
  1. #include <iostream>
  2. #include <iomanip>
  3. Using namespace std;
  4. int main( )
  5. {
  6. Double A=123.456, b=3.14159, c=-3214.67;
  7. cout<<setiosflags(iOS:: fixed) <<setiosflags(iOS:: Right) << setprecision(2);
  8. cout<<setw(ten) <<a<<endl;
  9. cout<<setw(ten) <<b<<endl;
  10. cout<<setw(ten) <<c<<endl;
  11. return 0;
  12. }
The output is as follows:
123.46 (field width 10, right-aligned, two decimal places)
3.14
-3214.67
First, set the fixed-point form output, take two decimal places, right-aligned. These settings are valid for subsequent outputs (unless reset), and SETW is only valid for subsequent output entries, so SETW (10) must be written before the output a,b,c.

Reproduced

C + + input cin and output cout

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.