How to use cin.get () in C ++?

1

I would like to know more about the cin.get () function and also the difference between it and the getline () function because whenever I go to search something related to this I get more search results talking about getline () do than cin.get ().

If you can ... leave a detailed answer:)

    
asked by anonymous 06.08.2017 / 23:34

1 answer

1

The method returns a type int with value equal to the character read in the output stream, not only for cin but for any istream .

char c = (char)cin.get() ;

The above code does the same as the following, which saves the character in a variable passed by reference, not by copy.

char c ;
cin.get( c ) ;

There is a third option that does the same.

char c ;
scanf( "%c" , &c ) ;

There are other methods and also functions with the same purpose, including differences such as the ability to capture only one key from the console (without having to enter a character to enter) and can still display the keyboard character. >

It also has several get methods of input streams that are used to capture other things. I'll list them from the source ( link ) and explain.

The int get() , as I explained, returns an integer that results from the capture of a character in the input stream.

The istream& get ( char& c ) , as I explained, saves by reference in the argument c the result of the capture of a character in the input stream. It also returns the used stream object itself if you want to re-invoke some method.

The istream& get ( char* s , streamsize n ) saves a string in an array s of characters (to catch the characters of the string when reaching the integer limit size n or when finding '\n' ) and returns the stream itself to invoke more methods on the same line.

The istream& get ( char* s , streamsize n , char delim ) saves a string in an array s of characters (to catch the characters of the string when reaching the integer limit size n or when finding the character delim ) and returns the stream itself to invoke more methods on the same line.

The methods istream& get ( streambuf& sb ) and istream& get ( streambuf& sb , char delim ) are like the previous two, but instead of saving in an array it saves in a buffer structure.

Any questions?

    
07.08.2017 / 21:33