Use CIN to obtain user input information

Source: Internet
Author: User

Cin (affiliated with istream) is a standard input channel for user input, corresponding to stdin of C. The operating system usually connects it to the keyboard to receive data input from the keyboard. Describes how CIN acquires user data from the keyboard:

Input from the keyboard is first sent"Input buffer"(Streambuf), Accept the variable and then"Input buffer.

"Input bufferUse the following code:

Streambuf * inputbuf = cin. rdbuf ();

Available< Get () Getline ()Three methods are used to obtain user input information.

 

Note: '\ t' -- tab ''-- space' \ n' -- enter

 

Some features of CIN:

(1) CIN can define strings by spaces, tabs, and carriage return, or customize characters.

(2) CIN becomes invalid in some cases (I .e., the user input cannot be obtained through cin to the specified accept variable, when you call the ">", "get", and "Getline" functions in Cin, the program will no longer be suspended for user input ).

(3) ">" use spaces, tabs, and carriage return to define the character escape.

By default, "get"/"Getline" uses a carriage return to define the Escape Character (used to read a row of data). You can also define custom characters.

 

For details, see the following analysis:

 

[<]

Int X;

Cin <X;

1. When you type "56" and press enter to end the input, X will be assigned a value of 56, and a "\ n" will remain in the "input buffer".

2. when you enter "\ t \ T32 \ t" and press enter to end the input, X is assigned to 32, and "residual" \ t \ n "in the input buffer is displayed.

3. When you type: "32 56 78" and press enter to end the input, X is assigned to 32, and "56 78 \ n" is left in the input buffer.

 

[Int get ()/istream & get (char & C )]/* Never expire */

If data exists in the input buffer, the first byte of the input buffer is taken out, the byte is returned, and the byte is deleted from the input buffer.

-- For example, if the "input buffer" is empty, this program will be suspended and waiting for user input.

-- For example, when "input buffer" is "\ n"

Int get () returns the '\ n' ASCII code value

Istream & get (char & C) returns the current CIN object, c = '\ n '.

Then, the '\ n' character is deleted from the "input buffer", and the current buffer is blank.

-- For example, when "input buffer" is "\ ts 56 \ t \ n"

Int get () returns the '\ t' ASCII code value

Istream & get (char & C) returns the current CIN object, c = '\ t '.

Then, the '\ t' character is deleted from the "input buffer", and the current buffer is changed to "s 56 \ t \ n ".

 

[Istream & get (char * s, int N)/istream & get (char * s, int N, char delim )]

Istream & get (char * s, int N) is defined by carriage return.

Istream & get (char * s, int N, char delim) uses 'Char delim 'to specify the delimiter.

Both functions obtain n-1 characters from the "input buffer", while s [n-1] is not used to receive data and will be invariably set to '\ 0 '.

/* Use istream & get (char * s, int N) as an example */

1. If the number of characters in the input buffer is m (excluding the last '\ n' carriage return) and less than n-1 characters, fill the remaining with' \ 0.

-- For example, if the current buffer is \ tab23 \ n, run get (CHS, 10). After the execution, CHS = "\ tab23 \ 0 \ 0 \ 0 \ 0\ 0", The buffer is changed to" \ n ", and the status of the CIN input stream is valid.

2. If the number of characters in the input buffer is 0 (excluding the last '\ n' carriage return ).

-- For example, if the current buffer is \ n, run get (CHS, 3). After the buffer is executed, CHS ="\ 0", The buffer changes to" \ n ", and the status of the CIN input stream changesInvalid.

3. If the number of characters in the input buffer is m (excluding the last '\ n' carriage return), it is equal to n-1 characters.

-- For example, if the current buffer is "sdrgh23 \ n", run get (CHS, 8). After execution, S = "sdrgh23\ 0", The buffer is changed to" \ n ", and the status of the CIN input stream is valid.

4. If the number of characters in the input buffer is m (excluding the last '\ n' carriage return), it must exceed n-1 characters.

-- For example, if the current buffer is "C ghy4nm \ n", run get (CHS, 5). After execution, S = "C GH\ 0", The buffer is changed to" y4nm \ n ", and the status of the CIN input stream is valid.

 

[Istream & Getline (char * s, int N)/istream & Getline (char * s, int N, char delim )]

Istream & Getline (char * s, int N) is defined by carriage return.

Istream & Getline (char * s, int N, char delim) uses 'Char delim 'to specify the delimiter.

Both functions obtain n-1 characters from the "input buffer", while s [n-1] is not used to receive data and will be invariably set to '\ 0 '.

/* Use istream & Getline (char * s, int N) as an example */

1. If the number of characters in the input buffer is m (excluding the last '\ n' carriage return) and less than n-1 characters, fill the remaining with' \ 0.

-- For example, if the current buffer is "\ tab23 \ n", run Getline (CHS, 10). After execution, S = "\ tab23 \ 0 \ 0 \ 0 \ 0\ 0", The buffer becomes null, and the status of the CIN input stream is valid.

2. If the number of characters in the input buffer is 0 (excluding the last '\ n' carriage return ).

-- For example, if the current buffer is \ n and Getline (CHS, 3) is executed, S = "\ 0 \ 0\ 0", The buffer becomes null, and the status of the CIN input stream is valid.

3. If the number of characters in the input buffer is m (excluding the last '\ n' carriage return), it is equal to n-1 characters.

-- For example, if the current buffer is "sdrgh23 \ n", run Getline (CHS, 8). After execution, S = "sdrgh23\ 0", The buffer becomes null, and the status of the CIN input stream is valid.

4. If the number of characters in the input buffer is m (excluding the last '\ n' carriage return), it must exceed n-1 characters.

-- For example, if the current buffer is "C ghy4nm \ n", run Getline (CHS, 5). After execution, S = "C GH\ 0", The buffer changes to" y4nm \ n ", and the status of the CIN input stream changesInvalid.

 

Failure-terrible nightmare

Failure [failbit] means that the receiving variable cannot obtain data from the input buffer, and the program will not be suspended to wait for the user's keyboard input.

When the receiving variable fails, it does not get any value and retains the original value.

In this case, you must reset the status to goodbit so that the receiving variable can obtain data from the input buffer.

PS: failbit is 4 goodbit is 0 and there are 2 more states: badbit eofbit

Obtain the CIN status

Int state = cin. rdstate ();

Bool isgood = cin. Good (); // whether it is in goodbit status

Bool isfail = cin. Fail (); // whether the status is failbit

Bool isbad = cin. Bad (); // whether it is in the badbit status

Bool iseof = cin. EOF (); // whether the status is eofbit

Set the CIN status

Int state = 0; cin. setstate (State );

Clear the CIN status // set it to goodbit

Cin. Clear ();

 

Cin will become invalid in the following three cases:

1. Type Mismatch

Initial status: "input buffer" is empty

Double D = 0.0;

Cin> D; // enter "SDR" on the keyboard and press Enter.

Because the number of Double Precision types cannot receive "SDR", it will become invalid, and the "input buffer" content is "SDR \ n ".

2. Empty data

Initial status: "input buffer" is empty

Char CHS [3] = {'A', 'B', 'C '};

Cin. Get (CHS, 3); // press enter directly without entering any data.

The switch fails, and the CHS = "\ 0bc" and "input buffer" content is "\ n ".

3. Insufficient escape length for received characters

Initial status: "input buffer" is empty

Char CHS [3] = {0 };

Cin. Getline (CHS, 3); // enter "sdrgh" on the keyboard and press Enter.

In this case, the CHS = "SD \ 0" and "input buffer" are "RGH \ n ".

 

(Full-text welcome comments)

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.