C + + get () function and getline () function use the detailed _c language

Source: Internet
Author: User

C + + get () function reads a character
The Get () function is a member function of a CIN input stream object, which has 3 forms: No parameters, one parameter, and 3 parameters.
1 Get function without parameters
Its invocation is in the form of

  Cin.get ()


Used to extract a character from a specified input stream, including a white-space character, and the return value of the function is the character being read. If you encounter a file terminator in the input stream, the function value returns the file ending flag eof (end of), typically 1 for EOF, with 1 instead of 0 or positive, taking into account that it is not confused with the ASCII code of the character, but the EOF values used by different C + + systems may be different.

[Example] reads characters with the Get function.

#include <iostream>
using namespace std;
int main ()
{
int c;
cout<< "Enter a sentence:" <<endl;
while ((C=cin.get ())!=eof)
cout.put (c);
return 0;
}


The operating conditions are as follows:

Enter a sentence:
I study C + + very Hard.↙ (enter a line of characters)
I study C + + very hard.       (Output the line character)
^z↙ (end of program)

The GetChar function in C language is the same as that of the flow member function cin.get (), C + + retains the use of C, and can read a word from the keyboard with GetChar (c) assignments to C.

2 A Get function with a parameter
Its invocation is in the form of

 Cin.get (CH)

The function is to read a character from the input stream and assign it to the character variable Ch. If the read succeeds, the function returns True (TRUE), and the function returns False (FALSE) if it fails (in the case of a file terminator). The example above can be rewritten as follows:

#include <iostream>
using namespace std;
int main ()
{
  char C;
  cout<< "Enter a sentence:" <<endl;
  while (Cin.get (c))//reads a word assignments to the character variable C, if the read succeeds, Cin.get (c) is true
  {cout.put (c);}
  cout<< "End" <<endl;
  return 0;
}

3 Get function with 3 parameters
Its invocation is in the form of
Cin.get (character array, number of characters N, terminating character)
Or
Cin.get (character pointer, number of characters N, terminating character)
The effect is to read n-1 characters from the input stream, assign to the specified array of characters (or the array to which the character pointer points), and end the read prematurely if the specified terminating character is encountered before the n-1 character is read. If the read succeeds, the function returns True (TRUE), and the function returns False (FALSE) if it fails (in the case of a file terminator). Rewrite The example 13.6 as follows:

#include <iostream>
using namespace std;
int main ()
{
  char ch[20];
  cout<< "Enter a sentence:" <<endl;
  Cin.get (ch,10, ' \\n ');//Specify line breaks as terminating characters
 cout<<ch<<endl;
  return 0;
}

The operating conditions are as follows:

Enter a sentence:
I study C + + very Hard.↙
I study

There are 22 characters in the input stream, but because the n specified in the Get function is 10, the n-1 (or 9) characters are read and assigned to the first 9 elements of the character array Ch. One might ask: Specify N-10, why read only 9 characters? Because a string is stored, a string-ending flag is added after 9 characters, which actually holds 10 characters in the array. Ask the reader to think: what happens if you don't add a string ending flag? The result is that when you use "cout<<ch" to output characters in an array, it is not the output-read string, but all the elements in the array. You can test for yourself what the value of ch[9] (that is, the 10th element in the array).

If you enter ↙

  Abcde

That is, the end character is encountered after the 9th character has been read, the read operation terminates, the first 5 characters have been stored in the array ch[0] to ch[4], and ch[5].

If the n specified in the Get function is 20 and 22 characters are entered, the first 19 characters in the input stream are assignments to the first 19 elements in the character array ch, and then a ' plus ' is added.

The 3rd parameter in the Get function can be written down, at which time the default is ' \ n '. The following two lines are equivalent:

  Cin.get (ch,10, ' \\n ');
  Cin.get (ch,10);


Terminating characters can also be in other characters. Such as

  Cin.get (ch,10, ' X ');


Stops the read operation when the character ' X ' is encountered.

C + + getline () function reads one line of characters
The role of the Getline function is to read a line of characters from the input stream, similar to a Get function with 3 arguments. That
Cin.getline (character array (or character pointer), number of characters N, Terminator flag character)

[Example] reads a line of characters with the Getline function.

#include <iostream>
using namespace std;
int main ()
{
  char ch[20];
  cout<< "Enter a sentence:" <<endl;
  cin>>ch;
  cout<< "The string read with a cin is:" <<ch<<endl;
  Cin.getline (ch,20, '/'); Read a character or meet the '/' End
  cout<< ' The second part is: <<ch<<endl;
  Cin.getline (ch,20); Read a character or meet ' n ' End
  cout<< "The third part is:" <<ch<<endl;
  return 0;
}

The program runs as follows:

Enter a sentence:i like c++./i study c++./i am Happy.↙ the
string read with cin is:i the
second part is:like C + + .
The third part IS:I study c++./i am H

Please analyze the running results carefully. Use "cin>>" to extract data from the input stream and terminate it with a space. So only one character ' I ' is read, stored in the character array element ch[0], and then stored ' in Ch[1 '. So when you use "cout<<ch" output, you output only one character ' I '. Then read 19 characters (or end) from the input stream with Cin.getline (CH, 20, '/'). Note that the data is not read at this time from the beginning of the input stream. There is a character pointer in the input stream that points to the character that should currently be accessed. At the beginning, the pointer points to the first character, and after reading the first character ' I ', the pointer moves to the next character (the space after ' I '), so the Getline function reads from the space and stops when it encounters the string "like C + +." In the 10 array elements that start with ch[0], and then output these 10 characters with "Cout<<ch". Note: Stop reading in the event of a Terminator flag character "/" is not placed in the array. The Cin.getline (CH, 20) is then read 19 characters (or the ' n ' End), because the '/' is not specified as the end sign, so the 2nd '/' is read as a common character, read 19 characters, and finally output the 19 characters.

There are several explanations and ask the reader to consider:
1 if the 2nd Cin.getline function is also written as a cin. Getline (CH, 20, '/"), what will the output be? The output of the last line at this time is:

  The third part IS:I study C + +.

2 If you are using Cin.getline (CH, 20, '/') to read data from the input stream, hit the return key ("\ n"), do you want to end the read? The conclusion is that at this time "\ n" is not an end sign "\ n" is read as a character.

3 When you use the Getline function to read characters from the input stream, when you encounter the end of the stop sign characters, and after the pointer moves to the Terminator character, the next Getline function begins to read from the next character of the terminating flag, as shown in the results of this program. If you use the Cin.get function to read characters from the input stream, the stop sign characters stops reading, the pointer does not move backwards, and still stays in the original position. The next read will still start at the end of the flag character. This is the difference between the Getline function and the Get function. If the two Cin.line function calls in the example program are changed to the following function call:

  Cin.getline (CH, 20, '/');


The result of the run is:

Enter a sentence:i like c++./i study c++./i am Happy.↙ "
string read with cin is:i"
second part is:like c+ +.  the third part is: (no valid characters are read from the stream)

A 2nd cin. Getline (CH, 20, '/') reads the character from the current position of the pointer, the 1th character encountered is the end of the stop sign character read in, and only the "ch[0" is stored in the "cout<<ch", so there is no character output when the output is "".

Therefore, use the Get function with special attention, if necessary, in other ways to skip the Terminator character (such as the Ignore function described later, see: Some of the IStream class member functions related to the input), but generally it is more convenient to use the Getline function.

4) Compare the difference between reading data using "cin<<" and Cin.getline () with a member function. When reading data with "cin<<", a blank character (including spaces, tab keys, enter key) is used as the Terminator flag, while reading data with Cin.getline () reads a series of characters consecutively, and can include spaces. With "Cin <<" You can read the standard types of data for C + + (if you are overloaded, you can also enter data for a custom type) and use Cin.getline () only to enter character data.

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.