Usage of functions such as Cin, Cin. Get (), Cin. Getline (), Getline (), and gets ()

Source: Internet
Author: User
 

When I was learning C ++, these input functions were a bit confusing. Here I will make a summary. I hope it will be helpful for later users to review them, if there are any errors, please give me more advice (all programs in this article are run through VC 6.0). repost the information of the author;
1. Cin
1. Cin. Get ()
2. Cin. Getline ()
3. Getline ()
4. Gets ()
5. getchar ()

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> A; cout <A <Endl;} input: jkljkljkl output: jkljkljkl input: jkljkl // In case of space, the output is 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: abcdeabcdeabcdeabcde (enter 25 characters) output: abcdeabcdeabcdeabcd (receiving 19 characters + 1 '\ 0 ')

 

Usage 3: cin. Get (No parameter) No parameter is used to discard unnecessary characters in the input stream, or discard the carriage return (Remove '\ 0' from the last line of CIN') To make up for the shortcomings of CIN. Get (character array name, number of characters received.

Method 1: CIN> year; cin. Get (); // or CIN. Get (CH); --- Remove '\ 0' of year'

Method 2: (CIN> year). Get (); // or (CIN> year. Get (CH); --- Remove '\ 0' of year'
 

 

3. Cin. Getline () // receives a string and 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: for kskr1, please input 2nd strings: kskr2. Please input 3rd strings: kskr3. output value of M [0]: kskr1. output value of M [1: kskr2 outputs the value of M [2]: kskr3

 

4. Getline () // receives a string. It can receive spaces and output the string. It must contain "# include <string>"

# Include <iostream >#include <string> using namespace STD; Main () {string STR; Getline (CIN, STR); cout <STR <Endl;} input: jkljkljkl output: jkljkljkl input: jkl jfksldfj jklsjfl output: jkl jfksldfj jklsjfl

 

Similar to CIN. Getline (), but cin. Getline () belongs to the istream stream, while Getline () belongs to the string stream. It is a different function.

5. Gets () // receives a string, receives spaces, and outputs the string. 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, and enter 3rd strings: kskr3 outputs M [0] value: kskr1 outputs M [1] value: kskr2 outputs 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 (); // cannot be written as getchar (CH ); cout <ch <Endl;} input: jkljkljkl output: J

 

// Getchar () is a C-language function, which can be compatible with C ++, but is not required or used as little as possible;

Usage of functions such as Cin, Cin. Get (), Cin. Getline (), Getline (), and gets ()

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.