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 the programs in this article are run through VC 6.0)
1. Cin
2. Cin. Get ()
3. Cin. Getline ()
4. Getline ()
5. Gets ()
6. getchar ()
Appendix: cin. Ignore (); cin. Get () // skip a character, for example, do not want to press enter, space, and other characters
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
Note:> it filters out invisible characters (such as spaces, carriage return, and Tab)
Cin> noskipws> input [J]; // if you do not want to skip the white space, use noskipws stream control.
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 () // 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 () // 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.