Example of using a stream member function for character input

Source: Internet
Author: User

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

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.