Detailed description of the cin standard input stream and the cin input stream

Source: Internet
Author: User

Detailed description of the cin standard input stream and the cin input stream

A standard input stream is used to input data to a program from an input device (such as a keyboard. in the header file iostream. the cin, cout, and cerr objects are defined in h. cin is the input stream, and others are the output stream.

In is the object of istream, which obtains data from the input device. the actual input process is: after the data is input, press enter, space, and other blank characters, the data is sent to the keyboard buffer, forming an input stream, and then through the extraction operator"
> "Extract data from the stream and assign values to the program.

Note that the data type is checked during the value assignment of cin. There is a status prompt in cin to indicate whether the cin works correctly. If the type does not match, the prompt is changed to '1 ', the normal value is '0'. Once it changes to '1', the cin cannot be assigned a value.

In this case, cin, clear (), cin. sync (), cin. ignor () is required ()

The following example is used to describe

# Include <iostream> using namespace std; int main () {int a; cout <"enter a character:" <endl; cin> a; // cin. clear (); // cin. sync (); cout <"failbit =" <cin. fail () <endl; // No error handling is performed. Output 1 cout <"enter a number:" <endl; cin> a; // cannot be entered, cout <"failbit =" <cin. fail () <endl; // either 1cin. clear (); // cin. sync (); cout <"failbit =" <cin. fail () <endl; // use cin. clear () restores the status identifier to normal '0' cout <"enter a number:" <endl; cin> a; // cannot be entered, although the status identifier is indicated by cin. clear () restores cin to normal, but the data in the buffer zone still exists. Therefore, the original character data is directly assigned to acut <"failbit =" <cin. fail () <endl; // another error occurs due to Type Mismatch. cin. clear (); cin. sync (); cout <"enter a number:" <endl; cin> a; cout <"failbit =" <cin. fail () <endl; // use cin. clear () resets the stream through cin. sync () clears the stream, that is, clears the buffer data, and then you can re-enter system ("pause"); return 0 ;}

Cin. clear () is used to reset the stream so that cin> can be assigned again, but the data in the stream is not cleared. therefore, cin. clear () and cin. sync.

 

Cin. ignor () and cin. sync () works similarly, but it does not clear the entire stream, but is partially cleared. It can be manually controlled, such as clearing the current row or clearing the line break at the end of the row. ignor is used to control the buffer data more accurately.

For example:

Cin. ignore (1024, '\ n') usually sets the first parameter to be large enough. In this way, only the second parameter' \ n' works, therefore, this statement clears the characters before the carriage return (including the carriage return) from the input buffer (Stream.

The following are some usage instructions:

Cin. ignore (numeric_limits <std: streamsize >:: max (), '/N'); // clear the current row

Cin. ignore (numeric_limits <std: streamsize >:: max (); // clear all content in cin

Do not be intimidated by long names. numeric_limits <std: streamsize>: max () is the maximum value used by the stream defined in the climits header file. You can also replace it with an integer that is large enough.

 

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.