Cin. Get (), Cin. Getline (), Getline (), gets (), getchar ()

Source: Internet
Author: User

-----------------------

1. Cin. Get ()

 

(1). Cin. Get () extract a single character. You can extract carriage return and space.

A = cin. Get ();

 

(2) Same as (1)

Cin. Get ();

 

(3 ).

Cin. Get (character array, number of characters N, Terminator character); // Terminator character can be left blank, default: '\ N'

Or cin. Get (character pointer, number of characters N, Terminator character); // The Terminator character can be left blank. The default value is '\ n'

 

The termination symbol cannot be skipped. You need to extract the carriage return '\ N'

Can be written

Cin. Get (A, 20 );

Cin. Get ();

You can also write them in combination.

Cin. Get (A, 20). Get ();

 

--------------------------------------------

2. Cin. Getline ()

Cin. Getline (character array/character pointer, number of characters N, Terminator character); // Terminator character can be left blank, default: '\ N'

 

You can skip the termination symbol.

That is, the line break generated by the Enter key is discarded when a whole row is read each time.

 

 

Returned value: this is to be studied.

---------------------------------------------

3. Getline ()

Getline () // receives a string, receives spaces, and outputs the string, and must contain "# include <string>"

 

 

# Include <iostream>

# Include <string>

Using namespace STD;

Void main (void)

{

Stringa;

Getline (CIN, );

Cout <A <Endl;

}

 

In the above Code, if you enter a B c, You need to press the Enter key twice to display a B c (in vc6), because Getline has three parameters, the third parameter is the string Terminator, that is, when Getline encounters this Terminator, it does not accept input, for example, Getline (CIN, St,'s '); even if the input is abcsabc, only ABC is displayed. Therefore, in the above case, some people say that Getline uses the carriage return ('\ n') as the terminator by default, press enter for the first time to end the string, and press enter for the second time to start output.

 

 

 

--------------------------------------------

4. Gets ()

Gets () // receives a string, receives spaces, and outputs the string, and must contain "# include <string>"

 

Returned value: gets (STR) returns STR when the read is successful. Str stores the successfully read content.

Returns NULL if the request fails, and the STR content remains unchanged.

 

# Include <iostream>

# Include <string>

Using namespace STD;

Int main ()

{

Chara [20];

Gets (a); // cannot be written as a = gets ();

Cout <A <Endl;

Return0;

}

 

Input: ABC

Output: ABC

 

Input: A B C

Output: A B C

 

Similar to an example in CIN. Getline (), gets () can also be used in multi-dimensional arrays:

 

# Include <iostream>

# Include <string>

Using namespace STD;

Int main ()

{

Chara [3] [20];

For (INTI = 0; I <3; I ++)

Gets (A [I]);

Cout <"--------" <Endl;

For (intj = 0; j <3; j ++)

Cout <A [J] <Endl;

Return0;

}

 

Input:

A B 1

A B 2

A B 3

Output:

A B 1

A B 2

A B 3

 

The usage of gets () and CIN. Getline () is similar, except that CIN. Getline () has one more parameter;

 

--------------------------------

 

5. getchar ()

Getchar () // accepts one character and must contain "# include <string>"

 

# Include <iostream>

# Include <string>

Using namespace STD;

Int main ()

{

Chara;

A = getchar ();

Cout <A <Endl;

Return0;

}

 

Input: ABC

Output:

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.