C + + Basics: Summary of various input methods, CIN, Cin.get (), Cin.getline (), Getline (), gets (), GetChar () __c++

Source: Internet
Author: User
  C + + Basics: Summary of various input methods, CIN, Cin.get (), Cin.getline (), Getline (), gets (), GetChar ()

Input principle Brief:

The input buffer of the program is built with a buffer. Each time the input process is the same, the input data is deposited into the input buffer when the keyboard input is finished


1.cin

Read Data according to the variable type of STH in Cin>>sth, where the variable type can be int,float,char,char*,string and many other types. This input operation ends when a terminator (space, Tab, Enter) is encountered and is not saved to the STH for the Terminator.

void Test_input () {
    char ch1[10],ch2[10];
    cout<< "Enter two strings:" <<endl;
    cin>>ch1;
    cin>>ch2;
    cout<< "Two strings are:" <<endl;
    cout<<ch1<<endl;
    cout<<ch2<<endl;
}



2.cin.get ( character array name, receive length, Terminator )

Where the Terminator means encountering the symbol end string read, the default is enter, and the maximum number of characters read is (length-1), because the last one is "\ n". Note that the Cin.get () operation encounters a terminator stop read, but does not discard the terminator from the buffer.


3.cin.getline (character array name, receive length, Terminator)

Its usage is very similar to cin.get (character array name, receive length, Terminator). Cin.get () When a long string is entered, it does not cause an error in the CIN function, and if a CIN operation is followed, it continues, just fetching data directly from the buffer. But Cin.getline () when the input is too long, causes the CIN function error, the subsequent CIN operation will no longer execute. The following code:

void Test_input () {
    char ch1,ch2[10];
    cout<< "Please enter a string:" <<endl;
    Cin.getline (ch2,6),//can receive up to 6-1 = 5 characters to CH2 without encountering a terminator
    cin>>ch1;
    cout<<ch2<<endl;
    cout<<ch1<< "\ n" << (int) ch1<<endl;
}

Test: The following figure, input Zifuchuan[enter], which is longer than the maximum length of 5, causes a CIN function error, and neither reads data directly from the input buffer as cin.get (), nor does it enter from the keyboard. Therefore, it can be noted here that when using Cin.getline (), it is appropriate to set its maximum acceptance length to a greater extent.



4.getline (IStream is,string str, terminator)

Similarly, the Terminator here is an optional argument (the default is still enter). However, the difference between getline () and a lot of the previous existence is that it is under the string library function, not the previous IStream stream, before all calls are preceded by a join of the #include<string>. Corresponding to this method read the second parameter is string type, but no longer char*, pay attention to the difference. In addition, the method does not end the input by encountering a space.

void Test_input () {
    string str;
    cout<< "Please enter string content:" <<endl;
    Getline (Cin,str, ' a ');
    cout<<str<<endl;
}

You can also see from the second diagram above that this method only ends when you encounter a terminator (here ' a ') and is insensitive to spaces and even carriage returns.



5. Gets (char *ch)

Gets () method also accepts a string, but unlike getline (), its argument is char*, not string, and if char ch is defined [n], the length is n, the sample note the length of the input string is not greater than n, otherwise it will be an error. The same gets () is not sensitive to spaces.

void Test_input () {char ch[10];
    cout<< "Please enter char* content:" <<endl;
    Gets (CH);
cout<<ch<<endl; 

OK, here it is, in fact, there are GetChar (), Getch (), and so on, here is not to repeat.

Reference blog: http://blog.csdn.net/livecoldsun/article/details/25489429

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.