Question about Terminator input with Cin cin. Get () getchar () and Getline

Source: Internet
Author: User
About using CIN
Cin. Get () getchar (), the terminator of Getline Input

Input values of CIN. Get (), getchar, Cin. Getline

 

1. How to handle the first character encountered by these functions

 

Cin. Get ()

 

# Include <iostream>

# Include <string>

Using namespace STD;

Int main ()

{

Char;

Char B;

A = getchar ();

B = cin. Get ();

Cout <A <B <Endl;

System ("pause ");

Return 0;

}

Input: A + space + B + press ENTER

Output

Input: A + press ENTER

It can be seen that the character B is blank or a carriage return.

Conclusion: When cin. Get () encounters a space at the beginning or press enter, it reads and assigns it to B.

 

Getchar ()

 

Replace the statements A and B with the following statements:

Char A, B;

A = getchar ();

B = getchar ();

Input: A + space + B + press ENTER

 

Input: A + press ENTER

 

It can be seen that the character B is blank or a carriage return.

Conclusion: getchar () is the same as cin. Get (). If a space at the beginning or press enter is used, it is directly read and assigned to B.

 

CIN

 

Replace the statements A and B with the following statements:

Char A, B;

A = getchar ();

Cin> B;

Input: A + space + B + press ENTER

 

Input: A + press ENTER + B + press ENTER

 

 

Conclusion: CIN can skip the leading space and press Enter.

 

Getline

 

Replace the statements A and B with the following statements:

Char;

String B

A = getchar ();

Getline (CIN, B );

Input: A + space + B + press ENTER

 



Input: A + press ENTER

 

 

Conclusion: Getline reads the leading space and carriage return. However, when it reads the carriage return, it is not assigned to B, but as the end identifier of B.

 

 

2. How to handle the terminator of these characters

String A, B, C;

Cin>;

B = getchar ();

Cout <A <B <Endl;

Input: A + press ENTER

 

Conclusion: The end character of CIN is the carriage return, which is left in the output queue and read by the following gechar. The Terminator will be retained in addition to Getline.

 

String A, B, C;

Getline (CIN, );

B = getchar ();

Cout <A <B <Endl;

Input: A + press ENTER + B + press ENTER

 

Conclusion: The end operator '/N' of Getline is discarded and not retained in the input queue.

 

To solve the unwanted characters left in the queue, we can solve this problem in two ways:

The first is to eat this character and call cin. Get () once ()

The second is to call cin. Ignore ();

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.