1. CIN:
how the input operation works , 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, which is why the input statement fails in the example!
Cin entered the end of the condition: Enter, Space, Tab. CIN handles these terminators: discards these characters in the buffer. different from Cin.get ().
Case 1:
#include <iostream>using namespace Std;int main () {int m, n;cin >> m;cin >> N; cout << m << n << endl;return 0;}
Test Normal Input:
To test for exception input:
Input of Case 2:string
void Main () {string str1;cin >> str1; Stop reading input of strings where spaces are encountered cout << str1 << endl;cin.get () getline (CIN, str1); cout << str1 << Endl ;}
Test:
It can be seen that CIN is encountering a "space" to stop reading the input, and Cin starts reading from the first "non-whitespace character";
The getline is read directly from the first character (whether it is not a space, read in), and getline is encountered "carriage return" to stop reading into;
When the above code to remove the Cin.get (), then enter "123"----"return, Getline directly read" Enter ", Run finished!
Case 3:
int main () {char str[8];cin.getline (str, 5), cout << str << endl;cin.getline (str, 5); cout << str << Endl;return 0;}
Test One:ABCD ( carriage return) ABCD (output) efgh (carriage return) efgh (output) when the user first enters the string character number less than 4 o'clock, the program executes normally!
Test Two: abcdefgh ( return) ABCD ( output -line feed) when the user first enters a number of characters greater than 4 o'clock, the first string takes the first four characters of the input, and the second input operation is not executed, The second string output is empty.
2, Cin.get ():
Case 1:
int main () {char Str1;char str2;str1 = Cin.get ();//Read single character, on screen input str2 = Cin.get (); cout << str1 << str2 << Endl Outputs the single character system ("pause") just loaded; To pause, or it will flash past}
Input: ABCD output: AB
Since Cin.get () is reading the first character, then why isn't str2 a?
The principle is as follows:
in the Cin object, there is a stream of characters stored, which can be imagined as a buffer, in fact, a thing encapsulated in CIN. When we enter a character on the program, the object CIN obtains the character we entered. For example, obtain ABCD and then pass. Get () The first character in the stream is removed and assigned to STR1, at which point the data stored in the CIN is BCD, while STR1 obtains a. When we run str2 = Cin.get () again, the same is true for the B of the data in the Cin stream to STR2, after which the data for the stream inside the CIN is a CD, and the str2 is B, so the final output is output AB.
There is another supplement, when will the data be entered? We can again through the above code to try, we enter a single letter ' a ', and then press ENTER, found that there is no output data, but wait for the input data again, we enter the letter ' B ', press ENTER after the output AB. Believe it, everyone should understand, because when we first enter a, through str1 = Cin.get () make the stream in CIN no data, empty. So when the second time is assigned to the STR2 value, it cannot find the data and re-enter the data. As you can see, when the stream data in CIN is emptied, it needs to be re-entered to assign a value.
Case 2:
{char str1[10], str2[10];cin >> str1;cin >> str2;cout << str1 << endl;cout << str2 << Endl;return 0;}
Test One input: Abcd[enter]efgh[enter] Output: ABCDEFGH the "parse" input encounters a carriage return end, which is normal.
Test Two inputs: ABCD Efgh Output: ABCDEFGH Parse reads a string the first time it encounters a space, it stops, and the ABCD Read the str1 and discard 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 a residual data room, and the read-in operation takes the data directly from the buffer.
Case 3:
int main () {char Str1;char str2;str1 = Cin.get ();//Read single character, input cin.get () on screen, str2 = Cin.get (); cout << str1 << str2 << Endl; Output the single character just loaded}
Input: ABCD output: AC
there are 3 cin.get () in the program, so, when the callback Cin.get () is empty, Cin.get automatically deletes a letter from the stream data in Cin, which acts as a delete.
Case 4:
}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 both characters return 0;}
Test One input: A[enter] Output: A-ten 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 Input: a b[enter] Output: a "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.
Case 5:
{Char ch, a[20];cin.get (A, 5), Cin >> ch;cout << a << endl;cout << (int) ch << endl;return 0;} </span>
Test One input: 12345[enter] Output: 123453 "Analysis" the first time the input is very long, the string was taken by length "1234" , and ' 5 ' remains in the buffer, so the second input character is not read from the keyboard, but instead takes ' 5 ' directly, so the ASCII value printed is 53 (the ASCII value of ' 5 ').
Test two inputs:1234[enter]a[enter] output:123497"Analysis" the second input is valid, indicating that the function of the first input after the enter discarded!
3, Cin.getline:
Cin.getline () with Cin.get (array_name,arsize) read in the same way, ending with enter, but accepting the space character. Reading a character by length (arsize) discards the last enter character. But these two functions are different: Cin.get (Array_Name, arsize) when the input string is very long, it will not cause the CIN function error, the subsequent CIN operation will continue, just fetch 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.
Char Str1[200];char str2[200];cin.getline (str1, sizeof (STR1), ' X '); With a single English letter ' X ' as the terminating identifier Cin.getline (str2, sizeof (STR2), ' Y '); Use a single English letter ' Y ' as the terminating identifier cout << "The first line is:" << str1 << Endl; Output cout << "The second line is:" << str2 << endl;system ("pause");}
4. CIN Exception:
iOS class defines these four constants Badbit, Eofbit, Failbit, goodbit, in fact, these four flags constant is to take the corresponding flag bit of the mask, that is, the input of four kinds of abnormal situation!
The values for the above four constants are:
Ios::badbit input (output) stream fatal error, irreversible
ios::eofbit has reached the end of the file
Ios::failbit input (output) stream with non-fatal error, can be redeemed
ios::goodbit Flow status is completely normal , each exception flag bit is 0
We can use output statements to verify the values of these constants:
cout << iOS:: Failbit << Endl; 2
cout << iOS:: Eofbit << Endl; 1
cout << iOS:: badbit << Endl; //4
cout << iOS:: Goodbit << Endl; 0
Cin.fail (), Cin.clear (), Cin.sync () Examples:
int main () {int A;while (true) {cin >> a;if (!cin)//condition can be rewritten as Cin.fail () {cout << "input type error, please reenter! "<< endl;cin.clear (); is a flag that sets all flags in CIN to the Active state cin.sync (); Empty stream}else{cout << a << endl;break;}} System ("Pause");}
The above cin default value is not 0, when the input is non-shaping, its status identifier is changed to fail (that is, 0), and then use Cin.clear () to change the error ID back to non-0, you can continue to enter, and then empty stream data continue to enter. If there is no cin.clear (), it will go to the dead loop, the process for us to enter the English alphabet, its status identification is fail, when running to the condition of judgment, always return to the wrong condition representation, and no way to input, because the wrong expression closed cin, so will enter the dead loop. You can comment out Cin.clear () and Cin.sync () separately for verification.
5, In.ignore ()
This function is used to discard the characters in the input buffer, the first parameter defines a number (_count), and the second parameter defines a character variable (_delim). Here's how the function performs: The function constantly takes a character from the buffer and determines if it is not _delim, if it is not dropped and counted, when the count reaches _count exit, and if it is, the drop character exits.
The default value of this function, the first parameter defaults to 1 , the second parameter defaults to EOF. So Cin.ignore () is the first character in a drop buffer.
Using Cin.get () to read characters, the Get function does not discard carriage returns, so the carriage return remains in the buffer, resulting in the second read of the data directly from the buffer to get a carriage return! Since Cin.get () does not automatically discard the carriage return at the end of the input, here we use the Ignore () function to manually discard the carriage return character!
< "<< C2 << Endl; Print two characters cout << (int) C1 << "" << (int) C2 << Endl; Print the ASCII value of both characters return 0;}
Empty the entire buffer :--- the function is most commonly used in this way, the first parameter is set very large, the second parameter is set to ' \ n ' , so that all the residual data in the carriage return in the buffer can be used, since it is not always possible to enter the remaining data in front of it, so it is reasonable to flush all the data in the buffer before a new input operation.
such as: Cin.ignore (1024x768, ' \ n ');
C + + input (CIN) detailed