![]() |
VOOZH | about |
cin is a predefined object in C++ used to accept input from the standard input stream (stdin). It works with the extraction operator (>>) to read data entered by the user and store it in variables.
Input
10Output
10cin >> var_name;
Here,
Note: cin is initialized when the program starts to make sure it is ready for input operations. It is also linked to cout to ensure that any buffered output is flushed before cin reads from the input stream.
The following examples demonstrate different ways of using `cin` for input operations in C++.
Example: Taking Text from User Input
Input
Welcome to GeeksforGeeksOutput
WelcomeExplanation
Example: Taking Multiple Inputs Using the Extraction Operator(>>) with cin
Input
ABC 13Output
Name : ABC
Age : 13
Explanation
cin Member Functions of cinThe cin object provides several member functions that offer greater control over input operations.
It reads an input character and stores it in a variable.
Input
Welcome to GFGOutput
Welcome to GFGExplanation
It reads a stream of characters of given length N into the string buffer. It stops when it has read (N - 1) characters or it finds the end of the file or newline character(\n).
Input
GeeksOutput
GeExplanation
It reads a stream of characters of given length N.
Input
Welcome to GFGOutput
Welcome toExplanation
It ignores or clears one or more characters from the input buffer.
Input
Enter a number and string:
8
Welcome to GFG
Output
You have entered:
8
Welcome to GFG
Explanation
The below table lists some commonly used member functions of cin in C++:
| Member Function | Description |
|---|---|
cin.get() | Reads a single character from the input stream, including whitespace. |
cin.getline() | Reads a line of text, including whitespace, and stops when it reaches a newline character. |
cin.ignore() | Ignores a specified number of characters or until a specified delimiter is encountered. |
cin.peek() | Returns the next character from the input stream without extracting it. |
cin.putback() | Puts a character back into the input stream. |
cin.eof() | Returns true if the end of the input stream has been reached. |
cin.fail() | Returns true if an input operation has failed (e.g., when input doesn't match the expected type). |
cin.clear() | Clears the error flags on the input stream, allowing further operations. |
cin.sync() | Discards unread characters from the input buffer. |
cin.gcount() | Returns the number of characters extracted by the last unformatted input operation. |
cin.rdbuf() | Gets or sets the associated stream buffer object for std::cin. |