Cin cin. Get cin. Getline

Source: Internet
Author: User
The end of CIN <is enter space tabcin. the get () end is only enter, but the enter will be placed in the queue waiting for CIN. the Getline () Terminator only has enter, but does not put enter into the queue.

If you use CIN>... If you have input before using cin. Getline, you need to process it. There are many processing methods. You can go to CIN>... Add the statements cin. Get ();.



Use Getline () to read the input line. When the program uses CIN to input data, use a blank character and a line terminator to separate the values. Use the Getline member function to read a whole line of text and separate different fields based on the value to be entered.

getline(char*line,int size,char ='/n')

The second parameter is the maximum number of characters read this time, and the third parameter is a delimiter that marks the end of reading a row.

The following function reads a line of text from the keyboard

#include <iostream>
using namespace std;

int main()

    char str[128];
    
    cin.getline(str,sizeof(str));
    cout <<"you typed :" <<str<<endl;
    

}

 

 

#include <iostream>
using namespace std;

int main()

    char str[128];
    cout<<"please input text and enter"<<endl;
    cin.getline(str,sizeof(str),'X');
    cout <<"you typed first line:" <<str<<endl;
    
    cin.getline(str,sizeof(str));
    cout <<"sencond line :"<<str<<endl;

}

Before entering X, you can press the Enter key once or multiple times without terminating the input of the first cin. Getline. The input operation of the first cin. Getline () will end with the first carriage return after typing X.

The difference between cin. Getline () and CIN> STR is that the former enters a row and the row can contain spaces, but the latter ends with a space or press enter as a string, excluding spaces.

 

Read a character with get ()

Get one character each time: Char istream: Get ();

Post: http://blog.csdn.net/lyac2006/article/details/1565556

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.