cin Reading starts with non whitespace characters , Everything from the first character that does not match the target type .
int n; int sum = 0; while (cin >> n) { sum += n; } cout << " Last input n The value of is : " << n <<
"\n"; cout << "sum The value of is : " << sum << "\n";
Entered “i” Characters that do not match the expected format , therefore cin >> n The result is false, Cycle termination

<> Flow state

member describe
eofbit If you reach the end of the file , Set the 1
badbit If the flow is broken , Set to 1, for example , File read error
failbit If the input operation fails to read the expected character or the output operation does not write the expected character , Set to 1
eof() If eofbit Is set , Return to true
bad() If badbit Is set , Return to true
fail() If badbit or failbit Is set , Return to true
clear() method , Will be cleared 3 States (eofbit, badbit, failbit), Set to 0

Why reset stream state ?
When the input does not match or reaches the end of the file , You need to use the clear() Reopen input .
int n; cout << " input n = "; cin >> n; cout << "n The value of is : " << n << "\n"; double d;
cout<< " input d = "; cin >> d; cout << "d The value of is : " << d << "\n";
First input n Time , Wrong content entered , Flow state failbit Set to 1, Directly affected the second cin input

Joined clear() Modification of
cout << "fail() = " << cin.fail() << "\n"; int n; cout << " input n = "; cin >> n;
cout<< "n The value of is : " << n << "\n"; cout << "fail() = " << cin.fail() << "\n"; cin.
clear(); cout << "=================== use cin.clear() after ====================" << "\n"
; cout << "fail() = " << cin.fail() << "\n"; double d; cout << " input d = "; cin >>
d; cout << "d The value of is : " << d << "\n"; cout << "fail() = " << cin.fail() << "\n";
The stream state has been cleared , Why? d Or not to input , The values are still uncertain ?


This is the first time the value we entered was not read , It's still in the input queue , Waiting to be taken out , here we are d When the , We don't need input directly , The value of the input queue is read , But it is not satisfied d Type of , So printing is an indeterminate value .

After the application clears the input queue :
cout << "fail() = " << cin.fail() << "\n"; int n; cout << " input n = "; cin >> n;
cout<< "n The value of is : " << n << "\n"; cout << "fail() = " << cin.fail() << "\n"; cin.
clear(); cout << "=================== use cin.clear() after ====================" << "\n"
; while (cin.get() != '\n') { continue; } cout <<
"=================== After clearing the input queue with =====================" << "\n"; cout << "fail() = "
<< cin.fail() << "\n"; double d; cout << " input d = "; cin >> d; cout << "d The value of is : " <<
d<< "\n"; cout << "fail() = " << cin.fail() << "\n";
while (cin.get() != ‘\n’)
{
continue;
}
get prototype ,int get(); Read a single character in the buffer , And return to its int type
The above code can be interpreted as , Clears the entire line of the buffer .

It can also be used :
while (!isspace(cin.get()))
continue;
prototype :int isspace ( int c ); // Determine whether it is a blank character , If yes, return No 0 value , Otherwise return 0
The difference from the previous method is that , This clear buffer can only clear one word .

<>get and getline Use of

get prototype :
int get();// Gets one character of the input queue
istream& get (char& c);// Gets a character of the input queue assigned to c
istream& get (char* s, streamsize n);
The first parameter is the address of the character pointer to be entered , The second parameter character pointer stores the number of characters ( Add at the end of the character ’\0’), The number of characters should also be added 1
istream& get (char* s, streamsize n, char delim);
The first two parameters are the same as above , The third parameter is the separator , The default version of the first two parameters is ’\n’ As a separator .

When the above function , Read the maximum number of characters or encountered a separator , Then stop
char name[30]; cout << " input name:"; cin.get(name, 30, '#');// Separator # cout << name <<
"\n"; cout << " View the contents of the input queue :" << (char)cin.get() << "\n";

char name[30]; cout << " input name:"; cin.getline(name, 30, '#');// Separator # cout <<
name<< "\n"; cout << " View the contents of the input queue :" << (char)cin.get();

The above two examples illustrate get and getline The difference between
get Leave the separator in the input queue ,getline It is discarded .

ignore() Clear the contents of the input queue
char c1; char c2; cout << " Enter two words :"; c1 = cin.get(); cin.ignore(255, ' ');
// Clear input queue contents , read 255 Characters or spaces encountered to stop clearing c2 = cin.get(); cout << " Two word initials :" << c1 << " " <<
c2<< "\n";

Input behavior

method behavior
getline(char*,int) If no characters are read ( Line breaks are not included ), Set to failbit
If the maximum number of characters is read , And there are other characters in the line , Set to failbit
get(char*,int) If no characters are read , Set the failbit

peek() Function returns the next character in the input , It does not extract characters from the input stream .
cin.peek();

gcount()
Function returns the last unformatted method read ( no >> Operator read ) The number of characters in . be similar to strlen() It's just , This method is based on the number of character records read in the input stream , than strlen() It's a lot slower .
cin.gcount();

putback() Function inserts a character into the input string , The first character of the next input statement is inserted .
The parameter is a character type
cin.putback(ch);

When inputting an array of characters , After exceeding the character array size , Set the last character to \0
char title[20]; char name[10]; cout << "name:"; cin.get(name, 10); if (cin.peek
() != '\n') { cout << " exceed 9 Character size \n"; } cout <<"name =" << name << "\n";
// Out of range , It sets the last character to \0 cout << "title:"; cin.get(title, 20); cout << "title =" <<
title<< "\n";

eof()
prototype
bool eof() const;
Determine whether to reach the end of the file , inspect eofbit Is it set ,eofbit=1 Then return true , Otherwise, return to false

Technology
©2019-2020 Toolsou All rights reserved,
c Language implementation 《 Student management system 》 No hole is the future of mobile phone ? There are still many problems to be solved Junior , A little sense , Just for mutual encouragement How to use Vue Used in Echarts Visual Library The 11th Blue Bridge Cup Java The second provincial competition B Group part Python- be based on OpenCV Contour fill for flooding algorithm hole filling 【C#】 The realization of student achievement information management system List Common interview questions in the collection and simple ideas China Mobile Science Popularization : Why do mobile networks call “ Cellular mobile network ”【Golang Basic series 10 】Go language On conditional sentences if