Cin in-depth analysis (I)-cin input operation Processing

Source: Internet
Author: User
Tags first string

Many beginners think that the CIN function is a very simple function! The CIN function has a lot of knowledge to know (for example, what is the return value of CIN, And what member functions are provided by CIN, such as cin. clear (), Cin. ignore (), Cin. fail (), Cin. good () and so on), if you do not have a good grasp of it, you may encounter problems when using it, but you do not know why! In addition, many people have indeed encountered many problems. Below are some simple examples:

Procedure 1:

# Include <iostream>

Using namespace STD;

Int main ()

{

Int M, N;

Cin> m;

Cin> N;

Return 0;

}

Test conditions:

If you enter two valid numbers each time, the program will not go wrong!

However, if you give an invalid input when you enter the 'A' character for the first time, for example, you will find that the program does not

The second input statement is executed. It seems a little strange !!

Procedure 2:

# Include <iostream>

Using namespace STD;

Int main ()

{

Char STR [8];

Cin. Getline (STR, 5 );

Cout <STR <Endl;

Cin. Getline (STR, 5 );

Cout <STR <Endl;

Return 0;

}

The function of the program is very simple, that is, input a string and then output it again. Program execution:

Test 1:

ABCD (Press ENTER)

ABCD (output)

Efgh (Press ENTER)

Efgh (output)

When the number of characters entered for the first time is less than 4, the program runs normally!

Test 2:

Abcdefgh (Press ENTER)

ABCD (output)

(Output-line feed)

When the number of characters entered for the first time is greater than 4, the first string accepts the first four characters, the second input operation is not executed, and the second string output is empty. It seems strange !!!

In fact, many times you will encounter such problems. If you are not familiar with the program input principle and the principles of some functions such as Cin, you do not know how to solve them! I will give a brief introduction here. It may not be very accurate or comprehensive, or there may be some misunderstandings. Please forgive me!

 

Input Operation Principle

Like the scanf function mentioned in the previous section, the input of a program has a buffer, that is, the input buffer. The input process is like this. When a keyboard input ends, the input data is stored in the input buffer, and the CIN function directly retrieves data from the input buffer. Because the CIN function retrieves data directly from the buffer zone, sometimes when there is residual data in the buffer zone, the CIN function will directly obtain the residual data without requesting keyboard input, this is why the input statement is invalid in the example!

Some Input Functions and operators of CIN

CIN is a extern istream object. Provides many available member functions and heavy-duty operators, such as: CIN <, Cin. Get (), Cin. Getline () and so on. Let's take a look at these functions:

I. Cin <

This operator reads data based on the type of the following variable.

Enter the end condition: Enter, space, and tab are displayed. (This is important !)

Processing of Terminator: discards the terminator (enter, space, Tab) that ends the input in the buffer)

 

Read characters:

Procedure 3:

# Include <iostream>

Using namespace STD;

Int main ()

{

Char C1, C2;

Cin> C1;

Cin> C2;

Cout <C1 <"" <C2 <Endl;

Return 0;

}

Test 1 input:

A [enter]

B [enter]

Output:

A B

Test 2 input:

A B [enter]

Output:

A B

Reading strings:

Procedure 4:

# Include <iostream>

Using namespace STD;

Int main ()

{

Char str1 [10], str2 [10];

Cin> str1;

Cin> str2;

Cout <str1 <Endl;

Cout <str2 <Endl;

Return 0;

}

Test 1 input:

ABCD [enter]

Efgh [enter]

Output:

ABCD

Efgh

[Analysis] it is normal to input a carriage return.

Test 2 input:

ABCD efgh

Output:

ABCD

Efgh

[Analysis] When a string is read for the first time, spaces are stopped. ABCD is read into str1, spaces are discarded, and the subsequent strings are given to the second string. This proves that the process of reading data from CIN ends with a space and discards the space character. There is a residual data room in the buffer zone, and the read operation directly retrieves data from the buffer zone.

II.Cin. Get ()

The function has three formats: No parameter, one parameter, and two parameters.

That is, Cin. Get (), Cin. Get (char ch), Cin. Get (array_name, arsize)

Read characters:

Enter the end condition: Enter

Processing the Terminator: Do not discard the enter in the buffer

Cin. Get () and CIN. Get (char ch) are used to read characters. Their usage is similar,

That is, CH = cin. Get () is equivalent to CIN. Get (Ch.

Procedure 5:

# Include <iostream>

Using namespace STD;

Int main ()

{

Char C1, C2;

Cin. Get (C1 );

Cin. Get (C2 );

Cout <C1 <"" <C2 <Endl; // print two characters

Cout <(INT) C1 <"" <(INT) C2 <Endl; // print the ASCII values of the two characters

Return 0;

}

Test 1 input:

A [enter]

Output:

A

97 10

[Analysis] only one input from the keyboard is executed. Obviously, the first character is 'A', and the second variable is enter (ASCII value: 10 ), this is because this function does not discard the enter character at the end of the last input, so the buffer left at the end of the first input is the enter character at the end of the last input!

Test 2 input:

A B [enter]

Output:

A

97 32

[Analysis] Obviously, the first character is 'A', and the second variable is space (ASCII value: 32 ). The reason is the same as above. The space character is not discarded.

Reading strings:

Cin. Get (array_name, arsize) is used to read strings and can accept space characters. When the input ends with enter, it reads characters according to the length (arsize) and discards the last enter character.

Procedure 6:

# Include <iostream>

Using namespace STD;

Int main ()

{

Char A [20];

Cin. Get (A, 10 );

Cout <A <Endl;

Return 0;

}

Test 1 input:

ABC def [enter]

Output:

ABC def

[Analysis] indicates that space is acceptable when the function inputs a string.

Test 2 input:

1234567890 [enter]

Output:

123456789

[Analysis] If the input is too long, the data is retrieved Based on the required length.

Procedure 7:

# Include <iostream>

Using namespace STD;

Int main ()

{

Char CH, a [20];

Cin. Get (A, 5 );

Cin> CH;

Cout <A <Endl;

Cout <(INT) CH <Endl;

Return 0;

}

Test 1 input:

12345 [enter]

Output:

1234

53

[Analysis] The first input is too long, and the string is "1234" in length, while '5' remains in the buffer. Therefore, the second input character is not read from the keyboard, the printed ASCII value is 53 (the ASCII value of '5 ).

Test 2 input:

1234 [enter]

A [enter]

Output:

1234

97

[Analysis] The second input is valid, indicating that the function discards the enter after the first input!

III.Cin. Getline ()

Cin. Getline () is similar to CIN. Get (array_name, arsize) in Read mode. It ends with enter and can accept space characters. When reading characters by length (arsize), the last enter character is discarded.

However, these two functions are different:

Cin. Get (array_name, arsize) when the input string is too long, it will not cause the CIN function error. Subsequent CIN operations will continue, but will retrieve data directly from the buffer zone. However, when the input of CIN. Getline () is too long, the CIN function will be wrong, and subsequent CIN operations will not be executed. (The specific cause will be detailed in the next section "Troubleshooting of CIN)

Program 8:

# Include <iostream>

Using namespace STD;

Int main ()

{

Char CH, a [20];

Cin. Getline (A, 5 );

Cin> CH;

Cout <A <Endl;

Cout <(INT) CH <Endl;

Return 0;

}

Test input:

12345 [enter]

Output:

1234

-52

[Analysis] and CIN. the get (array_name, arsize) routine will find that the CH here does not read 5 in the buffer, but returns-52. Here, the CIN> CH statement is not executed, this is because the CIN has an error! The next section will be detailed.

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.