The difference between cin.get () and Cin.getline () _c language

Source: Internet
Author: User
Tags int size

Cin.getline () and cin.get () are read-oriented rows-by-line reads, that is, reading the entire row at a time instead of a single digit or character, but there is a certain difference between the two.

Cin.get () reads one whole line at a time and leaves a newline character generated by the ENTER key in the input queue, for example:

Copy Code code as follows:

#include <iostream>
Using Std::cin;
Using Std::cout;
const int SIZE = 15;
int main () {
cout << "Enter Your name:";
Char Name[size];
Cin.get (name,size);
cout << "Name:" << name;
cout << "\nenter your address:";
Char Address[size];
Cin.get (address,size);
cout << "Address:" << address;
}

Output:
Enter your name:jimmyi shi
Name:jimmyi Shi
Enter your address:address:

In this example, Cin.get () reads the entered name into name and leaves the line feed "/n" generated by enter in the input queue (that is, the input buffer), so the next Cin.get () is found in the buffer. n ' and read it, resulting in a second inability to enter and read the address. The solution is to call Cin.get () after the first call to the Cin.get () to read the '/n ' character, which can be combined to write as Cin.get (name,size).

Cin.getline () reads an entire row at a time and discards the newline character generated by the ENTER key, such as:

Copy Code code as follows:

#include <iostream>
Using Std::cin;
Using Std::cout;
const int SIZE = 15;
int main () {
cout << "Enter Your name:";
Char Name[size];
Cin.getline (name,size);
cout << "Name:" << name;
cout << "/nenter your address:";
Char Address[size];
Cin.get (address,size);
cout << "Address:" << address;
}

Output:
Enter your name:jimmyi shi
Name:jimmyi Shi
Enter your Address:yn QJ
Address:yn QJ

Because the newline character generated by enter is discarded, the next cin.get () read to the address is not affected.
If Cin.get () is read in one character, but Cin.get () does not ignore any characters, the return character needs to be handled separately.

Two points Note:
(1) Learn to distinguish between get () and getline ();
(2) The newline symbol is \ n, not/n;

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.