OOPortal OOPortal

Operator Overloading  «Prev 

C++ input Class istream

Input is returned to an object of type istream as described in iostream.h. The operator >> is overloaded in this class to perform input conversions from standard types. The operator is left associative and returns a value of type istream&. The standard input istream corresponding to stdin is cin.
The effect of executing a simple input statement, such as cin >> x >> i; is to read from standard input (normally the keyboard) a value for x and then a value for i. White space is ignored.
The class istream contains public members such as:

istream& operator>>(int& i);
istream& operator>>(long& i);
istream& operator>>(double& x);
istream& operator>>(char& c);
istream& operator>>(char* s);
istream& get(char& c);
istream& get(char* s, int n, char c = '\n');
istream& getline(char* s, int n, char c = '\n');
istream& read(char* s, int n);

The member function get(char& c) inputs the character representation to c, white-space characters included.
The member function
get(char* s, int n, char c = '\n')
inputs at most n characters into the string pointed at by s, up to the specified delimiter character c or an end-of-file. 
The member function getline() works like get(char* s, int n, char c = '\n') except it discards rather than keeps the delimiter character in the designated istream.
The member function read(char* s, int n) inputs at most n characters into the string pointed at by s.
Since these are member functions, they can be used as follows:

cin.get(c);           //one character
cin.get(s, 40);       //length 40 or terminated by '\n'
cin.get(s, 10, '*')   //length 10 or terminated by *
cin.getline(s, 40)    //same as get but '\n' is discarded