C + + cin>> detailed

Source: Internet
Author: User

Reference Address: http://www.cnblogs.com/A-Song/archive/2012/01/29/2331204.html

The input of the program is built with a buffer, which is the input buffer. The first input process is this, when the input data is stored in the input buffer at the end of the keyboard input, and the CIN function takes the data directly from the input buffer. Because the CIN function takes data directly from the buffer, sometimes when there is residual data in the buffer, the CIN function directly obtains the residual data without requesting the keyboard input

#1:

#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;
}

Test:

ABCDEFGH (carriage return)

ABCD (output)

(Output-line wrapping)

"Analysis" for the first time after the completion of the direct program, rather than the second input, because the first multi-input data is still in the buffer, the second input is directly from the buffer cache without requesting keyboard input, the following discussion of several common input methods:

a . cin>>

The operator reads the data according to the type of the subsequent variable.

Enter end condition: Encounter Enter, Space, TAB key.

Handling of Terminator: Discard Terminator (Enter, Space, TAB) in the buffer that makes the input end

#2 :

#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:

ABCD EFGH

Output:

Abcd

Efgh

Parsing reads a string the first time it encounters a space, it stops, it reads ABCD into STR1, and discards the space, giving the next string a second string. This proves that CIN reads the data to the end of the space, and discards the whitespace, the buffer has residual data, and the read-in operation takes the data directly from the buffer.

two. cin.get (array name, length, Terminator)

Where the Terminator is an optional parameter, the number of characters read is up to (length-1), The Terminator specifies the end string to read the character, the default is enter

To read characters, direct cin.get (char ch) or ch=cin.get () can be

To read a character:

Enter end condition: Enter key

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

Cin.get () and Cin.get (char ch) are used to read characters, their use is similar,

That is, Ch=cin.get () is equivalent to Cin.get (CH).

#3 :

#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 value of these two characters
return 0;
}

Test one input:

A[enter]

Output:

A

97 10

Analysis will find that only one time from the keyboard input, obviously the first character variable takes the ' a ', the second variable takes the ENTER (ASCII value is 10), because the function does not discard the last input at the end of the Enter character, So at the end of the first input, the buffer remains the enter character at the end of the last input!

Test two inputs:

A b[enter]

Output:

A

97 32

"Analysis" Obviously the first character variable takes the ' a ', the second variable takes space (ASCII value is 32). For the same reason, the space character is not discarded.

To read a string:

Enter the end condition: The default enter key (so that you can accept a space, tab), you can customize the Terminator on the third parameter

Handle to Terminator: drop enter in buffer

#4 :

#include <iostream>
using namespace Std;
int main ()
{
Char ch, a[20];
Cin.get (A, 5, ' d ');
cin>>ch;
cout<<a<<endl;
cout<< (int) ch<<endl;
return 0;
}

Test one input:

12345[enter]

Output:

1234

53

"Analysis" The first time the input is very long, the string by the length of "1234", and the ' 5′ is still left in the buffer, so the second input character is not read from the keyboard, but instead of directly take the ' 5′, so the printed ASCII value is 53 (' 5′ ASCII value).

Test two inputs:

12d45[enter]

Output:

12

D

"Parse" The second output is D, indicating that the terminator in the buffer is not discarded when the custom terminator

three. cin.getline ()

Cin.getline (array name, length, Terminator) is roughly similar to Cin.get (array name, length, Terminator).

The difference is:

Cin.get () when the input string is very long, it does not cause an error in the CIN function, and subsequent CIN operations continue, just fetching data directly from the buffer. However, cin.getline () will cause an error in the CIN function when the input is very long, and the subsequent CIN operation will no longer execute.

#5 :

#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

Comparison of "Analysis" with Cin.get () will find that the CH does not read 5 in the buffer, but return 52, here in fact the CIN>>CH statement is not executed, because CIN error!

C + + cin>> detailed

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.