Usage of the CIN, Cin. Get (), Cin. Getline (), Getline (), and gets () functions in C ++

Source: Internet
Author: User

1,CIN

Usage 1: the most basic and common usage. Enter a number:

# Include <iostream>

Using namespace STD;

Main ()

{

Int A, B;

Cin> A> B;

Cout <a + B <Endl;

}

Input: 2 [Press enter] 3 [Press enter]

Output: 5

Usage 2: accept a string. When "space", "tab", and "Press ENTER" are all completed

# Include <iostream>

Using namespace STD;

Main ()

{

Char A [20];

Cin>;

Cout <A <Endl;

}

Input: jkljkljkl

Output: jkljkljkl

Input: jkljkl // end with Space

Output: jkljkl


2. Cin. Get ()

Usage 1: cin. Get (character variable name) can be used to receive characters

# Include <iostream>

Using namespace STD;

Main ()

{

Char ch;

Ch = cin. Get (); // or CIN. Get (CH );

Cout <ch <Endl;

}

Input: jljkljkl

Output: J

Usage 2: cin. Get (character array name, number of characters received) is used to receive a line of string, can receive Spaces

# Include <iostream>

Using namespace STD;

Main ()

{

Char A [20];

Cin. Get (A, 20 );

Cout <A <Endl;

}

Input: jkl

Output: jkl

Input: abcdeabcdeabcdeabcdeabcde (25 characters)

Output: abcdeabcdeabcdeabcd (receiving 19 characters + 1 '\ 0 ')

Usage 3: cin. get (No parameter) No parameter is mainly used to discard unnecessary characters in the input stream, or discard the carriage return to compensate for CIN. get (character array name, number of characters received) is insufficient.

I still don't know how to use this. If you know it, please enlighten me;


3. Cin. Getline ()

// Accept a string, which can receive spaces and Output

# Include <iostream>

Using namespace STD;

Main ()

{

Char M [20];

Cin. Getline (M, 5 );

Cout <m <Endl;

}

Input: jkljkljkl

Output: jklj

Accept 5 characters to m, and the last one is '\ 0'. Therefore, only 4 characters of output are displayed;

If you change 5 to 20:

Input: jkljkljkl

Output: jkljkljkl

Input: jklf fjlsjf fjsdklf

Output: jklf fjlsjf fjsdklf

// Extension:

// Cin. Getline () actually has three parameters: cin. Getline (accept the string to see the M, accept the number of 5, end character)

// When the third parameter is omitted, the default value is '\ 0'

// If you change cin. Getline () in the example to CIN. Getline (M, 5, 'A'), and output jklj when jlkjkljkl is input, output JK when jkaljkljkl is input.

When using multi-dimensional arrays, you can also use cin. Getline (M [I], 20) or the like:

# Include <iostream>

# Include <string>

Using namespace STD;

Main ()

{

Char M [3] [20];

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

{

Cout <"\ n enter the" <I + 1 <"string:" <Endl;

Cin. Getline (M [I], 20 );

}

Cout <Endl;

For (Int J = 0; j <3; j ++)

Cout <"output M [" <j <"] value:" <m [J] <Endl;

}

Enter 1st strings:

Kskr1

Enter 2nd strings:

Kskr2

Enter 3rd strings:

Kskr3

Output M [0] value: kskr1

Output M [1] value: kskr2

Output M [2] value: kskr3

4. Getline ()

// Accept a string, which can receive spaces and output. It must contain "# include <string>"

# Include <iostream>

# Include <string>

Using namespace STD;

Void main (void)

{

String st;

Cout <"input ST :";

Getline (CIN, St );

Cout <st <Endl;

}

In the above Code, if you enter ABC, You need to press the Enter key twice to display ABC (in vc6), because Getline has three parameters, and the third parameter is the string Terminator, that is, when Getline encounters this Terminator, it does not accept the input. For example, it is written as 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.


5. Gets ()

// Accept a string, which can receive spaces and output. It must contain "# include <string>"

# Include <iostream>

# Include <string>

Using namespace STD;

Main ()

{

Char M [20];

Gets (m); // cannot be written as M = gets ();

Cout <m <Endl;

}

Input: jkljkljkl

Output: jkljkljkl

Input: jkl

Output: jkl

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

# Include <iostream>

# Include <string>

Using namespace STD;

Main ()

{

Char M [3] [20];

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

{

Cout <"\ n enter the" <I + 1 <"string:" <Endl;

Gets (M [I]);

}

Cout <Endl;

For (Int J = 0; j <3; j ++)

Cout <"output M [" <j <"] value:" <m [J] <Endl;

}

Enter 1st strings:

Kskr1

Enter 2nd strings:

Kskr2

Enter 3rd strings:

Kskr3

Output M [0] value: kskr1

Output M [1] value: kskr2

Output M [2] value: kskr3

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

This section describes the examples of kskr1, kskr2, and kskr3 in this article. This example also applies to CIN> because there is no space entered here. If spaces are entered, for example, "Ks KR jkl [Press enter]", CIN will have received three strings: "Ks, KR, jkl "; for example, if "kskr 1 [Press enter] kskr 2 [Press enter]", then "kskr, 1, kskr" is received. This is not the result we want! Cin. Getline () and gets () do not generate this error because they can receive spaces;

6. getchar ()

// Accepts one character and must contain "# include <string>"

# Include <iostream>

# Include <string>

Using namespace STD;

Main ()

{

Char ch;

Ch = getchar ();

Cout <ch <Endl;

}

Input: jkljkljkl

Output: J

Reprint address: http://wenku.baidu.com/view/23058d12a21614791711283e.html

Related Article

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.