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 kindly advise (all in this articleProgramAll run through VC 6.0) reprinted, please keep the author information;
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>;
Cout <A <Endl;
}

Input: jkljkljkl
Output: jkljkljkl

Input: jkljkl// Ends with a 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 () // 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:
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 () // accepts a string.To receive and output spaces,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

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, which 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;

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.