How do you use Cin.get() in C++?

Asked

Viewed 3,318 times

2

i would like to know more about the Cin.get() function and also the difference between it and the getline() function because whenever I research something related to it I see more search results talking about getline() than Cin.get().

If you could... :)

1 answer

2


The method returns a type int value equal to the character read in the output stream, not exclusively 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 from the console only one key (without needing to enter to pick a character) and can still display or not the keyboard character.

It also has several methods get input streams that serve to capture other things. I will list them from the source (http://www.cplusplus.com/reference/istream/istream/get) and explain.

The int get(), as I explained, returns an integer that is the result of capturing a character in the input stream.

The istream& get ( char& c ), as I explained, saved by reference in the argument c the result of capturing a character in the input stream. Also, returns the stream object itself used if you intend to invoke another method.

The istream& get ( char* s , streamsize n ) saves a string in an array s of characters (stops capturing the characters of the string when it reaches 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 (stops capturing the characters of the string when it reaches 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 doubt?

  • So man, you clarified some of my doubts only that in the code below I did not understand why we need to pass as parameter the size of our vector in the call of the function Cin.get(): #include <iostream> #include <string> int main(){ char name[50]; Std::Cout << "What is your name? n"; Std::Cout << "My name is: "; Std::Cin.get(name,50); Std::Cout << "Hello " << name << " n"; }

  • 1

    It turns out that there are several methods with the same name whose signature differs by parameters. O name is not a char, it is an array of chars. In this case, it is calling istream::get( char* , int ), ie, will capture not only a character but a string (with the limit of 50 characters including the null character at the end) and save in name.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.