Stream member functions for character input:
1. The stream member function get has 3 forms: No parameter, 1 parameters, 3 parameters of
(1) get cin.get () Span style= "font-family: ' The song Body '; > Used to extract a character (including white space characters) from the specified input stream, the function's return value is the read-in character, and if a file terminator is encountered in the input stream, the function returns the value eof (EOF is in -1)
Note:eof:end of File,! EOF means that the read file does not end, to end the program in a while loop, press CTRL + Z ( the screen shows ^z) , and then Enter key.
(2) the get function of the 1 parameters is called in the form:cin.get (CH). The function is to read a character from the input stream, assign to the character variable ch, if the read succeeds the function returns a value other than 0 (true), if it fails (in case of a file terminator) , 0 is returned. Value (FALSE).
(3)3parameters ofGetfunction its invocation form:Cin.get (character array, number of charactersN, terminating character)orCin.get (character pointer, number of charactersN, terminating character). function is read from the input streamn-1Characters , assigned to the specified array of characters (or the array to which the character pointer points), if an-1characters before the specified terminating character is encountered, the read is ended prematurely. If the read succeeds, the function returns a non-0value (TRUE), such as failure (in case of file terminator), returns0value (FALSE).
2. read a line of characters with the member function getline
Cin.getline (character array (or character pointer), number of characters n, terminating flag character )
such as:Cin.getline (ch,20,'/');
Example : Use the get function to read in characters.
Program:
#include <iostream>
using namespace Std;
int main ()
{
int C;
cout << "Enter a sentence:" << Endl;
while ((c = cin.get ())! = EOF)
{
Cout.put (c);
}
System ("pause");
return 0;
}
Results:
Enter a sentence:
Yaoyao clever!
Yaoyao clever!
^z
Please press any key to continue ...
This article is from the "Rock Owl" blog, please be sure to keep this source http://yaoyaolx.blog.51cto.com/10732111/1763824
Example of using a stream member function for character input