Home c++ cin.get () Function

cin.get () Function

Author

Date

Category

Please explain to me the cin.get () function. I read about him in Daytel, but there it is somehow not very detailed and it is well described. Can someone tell me in more detail, if not difficult?


Answer 1, authority 100%

When the get () method is called with an argument of type char or arguments in general, it retrieves the next input character, even if it is a space, tab, or newline character. The get (char & amp; ch) version assigns the input character to its argument, and the get (void) version simply uses the input character, converts it to an integer type (usually int) and returns this value.

Plus a few words about a couple more overloaded varieties:

istream & amp; get (char * buf, streamsize num);
istream & amp; get (char * buf, streamsize num, char delim);

The first form of the get () function reads characters into the array pointed to by the buf pointer until num – 1 characters are read, a line break is found, or end of file has been reached. This function writes a null character to the end of the array pointed to by the buf pointer. The newline character is not read (!). It remains in the stream until the next input is performed.

The second form of the get () function reads characters into the array pointed to by the buf pointer until num - 1 characters have been read, character delim encountered or end of file reached. The function writes a null character to the end of the array pointed to by the buf pointer. The delim character is not read (!). It remains in the stream until the next input is performed.


Answer 2, authority 33%

int istream :: get ();

Extracts a character from the stream and returns its value (cast to integer).

If an error occurs, then flags are set in the stream (for example, cin ):

eofbit – Attempt to read when the end of the stream is reached.

failbit – no characters were extracted because either the end of the stream was reached or the insert operation at the destination failed (this only applies to streambuf ).

badbit – in case of other errors.

The good () function can be used to test these flags.

Documentation at cplusplus.com.
Help in Russian on cppreference.com


Streams are also converted to the bool type, which allows you to write the following code:

istream in;
...
string s;
while (in & gt; & gt; s) // operator & gt; & gt; will return a link to the stream, which is converted to bool
{
  cout & lt; & lt; s & lt; & lt; '';
}

Answer 3

The cin.get () command is another function call that reads data from the input data stream and waits for the ENTER key to be pressed.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions