C ++ Input and Output

Source: Internet
Author: User

C ++ Input and Output

Reposted in this article: blog.csdn.net/zhanghaotian2011/article/details/8868577blog

The input and output are not formal components in the C ++ language. C and C ++ do not provide a special statement structure for the input and output. defined, it is defined in the I/O library provided by the compilation system 。
The output and input of C ++ are implemented in stream mode. Figure 3.2 and Figure 3.3 indicate the process of C ++ inputting and outputting through stream 。

The definitions of the stream object 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, to facilitate the description, the input statement implemented by the ">" Operator between cin and stream extraction is often called an input statement or a cin statement, the statements output by the cout and stream insertion operators <are called output statements or cout statements. According to the C ++ syntax, all operations that can be implemented and end with a semicolon are statements. 1. The general format of the cout statement for basic operations of the input stream and output stream 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, first, 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 encountered, at this time, the existing data in the buffer zone is output together, and the buffer zone is cleared. The data in the output stream is output on the default system device (usually the display 。
A cout statement can be written into several lines.
Cout <"This is a simple C ++ program." <endl;
Can be written
Cout <"This is" // note that there is no semicolon at the end of the line
<"A C ++"
<"Program ."
<Endl; // The statement ends with a semicolon
You can also write multiple cout statements, that is
Cout <"This is"; // There is a semicolon at the end of the statement
Cout <"a C ++ ";
Cout <"program .";
Cout <endl;
In the above three cases, the output is
This is a simple C ++ program.
Note that you cannot use the 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, you do not have to notify the computer of the type of output. The system will automatically identify the type of output data, output data according to the corresponding type. For example, if a is defined as int type, B is float type, and c is char type
Cout <a <''' <B <''' <c <endl;
Output in the following form:
4 345.789
Similar to cout, A cin statement can be written into several lines.
Cin> a> B> c> d;
Can be written
Cin> a // note that there is no semicolon at the end of the row
> B // The write may look clearer.
> C
> D;
It can also be written
Cin>;
Cin> B;
Cin> c;
Cin> d;
In the above three cases, you can enter the following from the keyboard: 1 2 3 4
You can also input data in multiple rows:
1
2 3
4
When using cin, the system also extracts bytes of the corresponding length from the input stream based on the variable type.
Char c1, c2;
Int;
Float B;
Cin> c1> c2> a> B;
If you enter
1234 56.78
Note: There should be spaces behind 34 to separate them from 56.78. You can also enter them in the following format:
1 2 34 56.78 (there is a space between 1 and 2)
Do not use the cin statement to enter space characters or carriage return linefeeds as character variables and 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 the data in the corresponding format, otherwise, errors may occur. 2. The default format of cout and cin is used in the input and output streams, for example, if the field width is specified when the real number is output, only two decimal places are reserved, data is aligned to the left or right. C ++ provides control operators (manipulators in some books) used in the input and output streams )。
Note: If a controller is used, you must add the iomanip header file in addition to the iostream header file at the beginning of the Program unit 。
Example: number of outputs with double precision 。
Double a = 123.456789012345; returns an initial value to.
(1) cout <a; output: 123.456
(2) cout <setprecision (9) <a; output: 123.456789
(3) cout <setprecision (6); restore the default format (precision is 6)
(4) cout <setiosflags (ios plugin fixed); output: 123.456789
(5) cout <setiosflags (ios completed fixed) <setprecision (8) <a; output: 123.45678901
(6) cout <setiosflags (ios runtime scientific) <a; output: 1.234568e + 02
(7) cout <setiosflags (ios audio scientific) <setprecision (4) <a; output: 1.2346e02
The following is an example of integer output:
Int B = 123456; returns an initial value to B.
(1) cout <B; output: 123456
(2) cout (3) cout <setiosflags (ios container uppercase) <B; output: 1E240
(4) cout <setw (10) <B <',' <B; output: 123456,123456
(5) cout <setfill ('*') <setw (10) <B; output: *** 123456
(6) cout <setiosflags (ios container showpos) <B; output: + 123456
If the same setw (n) is used in multiple cout statements and setiosflags (ios secure right) is used, the right alignment of each row of data can be realized. If the same precision is specified, the upper and lower decimal points can be aligned 。
For example, the number of decimal lines in the row is 3.1 。
# Include <iostream>
# Include <iomanip>
Using namespace std;
Int main ()
{
Double a = 123.456, B = 3.14159, c =-3214.67;
Cout <setiosflags (ios plugin fixed) <setiosflags (ios plugin right) <setprecision (2 );
Cout <setw (10) <a <endl;
Cout <setw (10) <B <endl;
Cout <setw (10) <c <endl;
Return 0;
}
The output is as follows:
123.46 (the width of the field is 10, right-aligned, with two decimal places)
3.14
-3214.67
First, set the fixed-point output, take two decimal places, and align right. These settings are valid for the subsequent output (unless reset), and setw is only valid for the next output, therefore, setw (10) must be written before Output a, B, and c )。

When I was learning C ++, these input functions were a bit confusing. Here I will make a summary. I hope it will be helpful for later users to review them, if there are any errors, please give me more advice (all programs in this article are run through VC 6.0). repost the information of the author;
1. cin
1. cin. get ()
2. cin. getline ()
3. getline ()
4. gets ()
5. getchar ()

1. cin>

Usage 1: the most basic and common usage. Enter a number:

# Include <iostream>
Using namespace std;
Main ()
{
Int a, B;
Cin> a> B;
Cout <a + B <endl;
}

Input: 2 [Press enter] 3 [Press enter]
Output: 5

Usage 2: accept a string. When "space", "TAB", and "Press ENTER" are all completed

# Include <iostream>
Using namespace std;
Main ()
{
Char a [20];
Cin>;
Cout <a <endl;
}

Input: jkljkljkl
Output: jkljkljkl

Input: jkljkl // end with Space
Output: jkljkl

2. cin. get ()

Usage 1: cin. get (character variable name) can be used to receive characters

# Include <iostream>
Using namespace std;
Main ()
{
Char ch;
Ch = cin. get (); // or cin. get (ch );
Cout <ch <endl;
}

Input: jljkljkl
Output: j

Usage 2: cin. get (character array name, number of characters received) is used to receive a line of string, can receive Spaces

# Include <iostream>
Using namespace std;
Main ()
{
Char a [20];
Cin. get (a, 20 );
Cout <a <endl;
}

Input: jkl
Output: jkl

Input: abcdeabcdeabcdeabcdeabcde (25 characters)
Output: abcdeabcdeabcdeabcd (receiving 19 characters + 1 '\ 0 ')

Usage 3: cin. get (No parameter) No parameter is mainly used to discard unnecessary characters in the input stream, or discard the carriage return to compensate for cin. get (character array name, number of characters received) is insufficient.

I still don't know how to use this. If you know it, please enlighten me;

3. cin. getline () // receives a string and can receive spaces and Output

# Include <iostream>
Using namespace std;
Main ()
{
Char m [20];
Cin. getline (m, 5 );
Cout <m <endl;
}

Input: jkljkljkl
Output: jklj

Accept 5 characters to m, and the last one is '\ 0'. Therefore, only 4 characters of output are displayed;

If you change 5 to 20:
Input: jkljkljkl
Output: jkljkljkl

Input: jklf fjlsjf fjsdklf
Output: jklf fjlsjf fjsdklf

// Extension:
// Cin. getline () actually has three parameters: cin. getline (accept the string to see the m, accept the number of 5, end character)
// When the third parameter is omitted, the default value is '\ 0'
// If you change cin. getline () in the example to cin. getline (m, 5, 'A'), and output jklj when jlkjkljkl is input, output jk when jkaljkljkl is input.

When using multi-dimensional arrays, you can also use cin. getline (m [I], 20) or the like:

# Include <iostream>
# Include <string>
Using namespace std;

Main ()
{
Char m [3] [20];
For (int I = 0; I <3; I ++)
{
Cout <"\ n enter the" <I + 1 <"string:" <endl;
Cin. getline (m [I], 20 );
}

Cout <endl;
For (int j = 0; j <3; j ++)
Cout <"output m [" <j <"] value:" <m [j] <endl;

}

Enter 1st strings:
Kskr1

Enter 2nd strings:
Kskr2

Enter 3rd strings:
Kskr3

Output m [0] value: kskr1
Output m [1] value: kskr2
Output m [2] value: kskr3

4. getline () // receives a string. It can receive spaces and output the string. It must contain "# include <string>"

# Include <iostream>
# Include <string>
Using namespace std;
Main ()
{
String str;
Getline (cin, str );
Cout <str <endl;
}

Input: jkljkljkl
Output: jkljkljkl

Input: jkl jfksldfj jklsjfl
Output: jkl jfksldfj jklsjfl

Similar to cin. getline (), but cin. getline () belongs to the istream stream, while getline () belongs to the string stream. It is a different function.

5. gets () // receives a string, receives spaces, and outputs the string. It must contain "# include <string>"

# Include <iostream>
# Include <string>
Using namespace std;
Main ()
{
Char m [20];
Gets (m); // cannot be written as m = gets ();
Cout <m <endl;
}

Input: jkljkljkl
Output: jkljkljkl

Input: jkl
Output: jkl

Similar to an example in cin. getline (), gets () can also be used in multi-dimensional arrays:

# Include <iostream>
# Include <string>
Using namespace std;

Main ()
{
Char m [3] [20];
For (int I = 0; I <3; I ++)
{
Cout <"\ n enter the" <I + 1 <"string:" <endl;
Gets (m [I]);
}

Cout <endl;
For (int j = 0; j <3; j ++)
Cout <"output m [" <j <"] value:" <m [j] <endl;

}

Enter 1st strings:
Kskr1

Enter 2nd strings:
Kskr2

Enter 3rd strings:
Kskr3

Output m [0] value: kskr1
Output m [1] value: kskr2
Output m [2] value: kskr3

The usage of gets () and cin. getline () is similar, except that cin. getline () has one more parameter;

This section describes the examples of kskr1, kskr2, and kskr3 in this article. This example also applies to cin> because there is no space entered here. If spaces are entered, for example, "ks kr jkl [Press enter]", cin will have received three strings: "ks, kr, jkl "; for example, if "kskr 1 [Press enter] kskr 2 [Press enter]", then "kskr, 1, kskr" is received. This is not the result we want! Cin. getline () and gets () do not generate this error because they can receive spaces;

6. getchar () // accepts one character and must contain "# include <string>"

# Include <iostream>
# Include <string>
Using namespace std;
Main ()
{
Char ch;
Ch = getchar (); // cannot be written as getchar (ch );
Cout <ch <endl;
}

Input: jkljkljkl
Output: j

// Getchar () is a C-language function, which can be compatible with C ++, but is not required or used as little as possible;

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.