China

China

>> CIN
COUT <<
like from water flowing down.

cin desired data can be continuously read from the keyboard, a space, tab or line as a separator.

cin encountered delimiter (such as line breaks may be the default, it can be self-defined, such as a comma) stops, but does not remove the separator, if it continues to read will read the separator. But cin to read each read the front line breaks, tab and space are ignored (encounter delimiters considered complete once read).

Do not want to skip the whitespace characters, then use noskipws flow control. For example, cin >> noskipws >> input;

cin-readable string.

cin.get

cin.get not ignore the front of the space, tab and newline, and will remain delimiters in the input buffer. To delete a separator, you can add a cin.get that end (), brackets can not write anything.
cin.get (char var) if successful returns cin objects can be chained to support operations, such as cin.get (b) .get ().

cin.get read a single character

	char a;
	cin.get(a);

cin.get read a line

Read one line may be used istream & get (char * s, streamsize n) or istream & get (char * s, size_t n, streamsize delim). The difference is in the default newline former, which can be specified terminator. n represents the size of the target space.

	char array[20] = { NULL };
	cin.get(array,20,';');

When reading a row, the second parameter n is not the default. The third parameter may be a default, default to default newline can be specified delimiter.
If n is reached or until the string delimiters encountered than arrays of variable length - 1, then the debug error. n should be less than the variable length of the array -1.

cin.getline

cin.getline encountered delimiter stopped and will delete the buffer separator.

char array[20] = { NULL };
cin.getline(a, 20); //默认以换行符结束
cin.getline(a, 20, ',');

When reading a row, the second parameter n is not the default. The third parameter may be a default, default to default newline can be specified delimiter.
If n is reached or until the string delimiters encountered than arrays of variable length - 1, then the debug error. n should be less than the variable length of the array -1.

cin clear the error message and empty the buffer

When entering, probably did not finish because of a row over the length of the array of characters to be input and phase occurs. At this time, cin error flag will be set to 0, and this line because there is residual in a portion of the input buffer, not properly read the next line. At this point need to clear the error flag, and clear the buffer.

cin.clear();  // 清除错误标志   
cin.ignore(numeric_limits<std::streamsize>::max(),'\n'); //清除缓冲区的当前行

Described in detail as follows:

Clear error message

When reading the keyboard inputs CIN, error inevitably occurs, once the error, the CIN status setting condition (condition state). Conditions state identification symbol:
goodbit: No error
eofbit: has reached the end of the file
failbit: non-fatal I / O error, concerned to restore
badbit: fatal I / O error and can not recover
if the need to increase the input and output class iOS.: : identification symbol. These conditions and corresponding to the state is set, read a stream object member function and state determination condition. They mainly include:
s.eof (): if set eofbit s stream, then returns to true;
s.fail (): if set failbit s stream, then returns to true;
s.bad (): If the stream s badbit set, returns to true;
s.good (): If the set flow goodbit s returns to true;
s.clear (flags): Clear the status flags, and flags the given flag bit is set to 1, return void.
s.setstate (flags): the given condition state flags flag corresponding to the flow condition state s in position 1, returns void.
s.rdstate (): returns the current state of the stream s condition, the return type strm :: iostate. strm :: iostate associated shaping machine name, defined by each iostream class, used to define the condition state.

Learn about the state of the above conditions associated with the input stream of the operating function, the following conditions to see a status bit failbit not been read by the input buffer set is caused, by example and then clear () reset.

#include <iostream>
using namespace std;

int main()
{
char ch, str[20]; 
    cin.getline(str, 5);
    cout<<"flag1:"<<cin.good()<<endl;    // 查看goodbit状态,即是否有异常
    cin.clear();                         // 清除错误标志
    cout<<"flag1:"<<cin.good()<<endl;    // 清除标志后再查看异常状态
    cin>>ch; 
    cout<<"str:"<<str<<endl;
    cout<<"ch :"<<ch<<endl;

    system("pause");
    return 0;
}

Input: 12345 [ENTER], output is:
Write pictures described here

As can be seen, because the input buffer has not been read input error caused by the clear () to clear the abnormal state of the input stream object cin. , Does not affect the latter cin >> ch read data from the input buffer. Because then read cin.getline string remaining in the input buffer is: 5 [Wrap], cin >> ch thus read and stored in 5 ch, the input and print output 5.

If the clear () comments, cin >> ch; will read failure, ch is empty.
cin.clear () is equivalent to cin.clear (ios :: goodbit); because cin.clear () default parameters is ios :: goodbit, so no display transfer, and therefore you see most often is:
cin.clear ().

Empty input buffer

When we think of making an input, the input buffer is emptied and reset state conditions. State reset condition using Clear (), clear the input buffer should be used:
the function prototype: istream & ignore (streamsize num = 1, int delim = EOF);
function operates: Skip n characters in the input stream, or in the specified face the early termination (this time skip several characters including the terminating character including) termination character.
Use the following examples

#include <iostream>
using namespace std;

int main()
{
    char str1[20]={NULL},str2[20]={NULL};
    cin.getline(str1,5);
    cin.clear();  // 清除错误标志   
    cin.ignore(numeric_limits<std::streamsize>::max(),'\n'); //清除缓冲区的当前行
    cin.getline(str2,20);
    cout<<"str1:"<<str1<<endl;
    cout<<"str2:"<<str2<<endl;
    system("pause");
    return 0;
}

Program input: 12345 [ENTER] Success [ENTER], program output:
Here Insert Picture Description
Note:
(1) used in the program cin.ignore emptied current line input buffer the input data in the last residue did not affect the once entered, this is the ignore () function in the main role. Wherein, numeric_limits :: max () is the maximum flow, but the use of header files defined, you can also use a sufficiently large integer instead.
If you want to clear the input buffer, remove the line breaks, use:
cin.ignore (numeric_limits <std :: streamsize> :: max ()); cin clear in everything.

(2) cin.ignore (); when there is no data input buffer will block waiting for the arrival of data.

(3) have a question, call the Internet a lot of information that cin.sync () to clear the input buffer, I tested a bit, VC ++ can, but using GNU C under linux ++ but it's not desperation, under linux on the choice the cin.ignore ().

Published 52 original articles · won praise 0 · Views 679

Guess you like

Origin blog.csdn.net/UniversityGrass/article/details/104687529