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.
- Used to read input from the keyboard during program execution
- Works with the extraction operator (>>) to extract data from the input stream
- Supports reading different data types such as integers, characters, strings, and floating-point values
#include <iostream>
using namespace std;
int main() {
// Variable to store data given by cin
int a;
// Take input using cin
cin >> a;
cout << a;
return 0;
}
Input
10Output
10Syntax of cin
cin >> var_name;
Here,
- cin is the standard input stream object
- >>: It is the extraction operator to extract data from cin.
- var_name: It is a variable that will store the input data provided by the user.
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.
Examples of cin
The following examples demonstrate different ways of using `cin` for input operations in C++.
Example: Taking Text from User Input
#include <iostream>
using namespace std;
int main() {
// Variable to store data given by cin
string s;
// Take input using cin
cin >> s;
// Print output
cout << s;
return 0;
}
Input
Welcome to GeeksforGeeksOutput
WelcomeExplanation
- cin reads input until the first whitespace character is encountered
- Only the word "Welcome" is stored in the string variable
- The remaining words are not read by this input operation
Example: Taking Multiple Inputs Using the Extraction Operator(>>) with cin
#include <iostream>
using namespace std;
int main() {
string name;
int age;
// Take multiple input using cin
cin >> name >> age;
cout << "Name : " << name << endl;
cout << "Age : " << age << endl;
return 0;
}
Input
ABC 13Output
Name : ABC
Age : 13
Explanation
- Multiple values can be read using a single cin statement
- Each value is extracted sequentially from the input stream
- The first value is stored in name and the second value is stored in age
cin Member Functions of cin
The cin object provides several member functions that offer greater control over input operations.
cin.get()
It reads an input character and stores it in a variable.
#include <iostream>
using namespace std;
// Driver Code
int main()
{
char ch[30];
cin.get(ch, 25);
// Print ch
cout << ch;
}
Input
Welcome to GFGOutput
Welcome to GFGExplanation
- cin.get() reads characters from the input stream, including whitespace
- It stores the extracted characters in the specified character array
- Input continues until the specified limit is reached or a delimiter is encountered
cin.getline()
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).
#include <iostream>
using namespace std;
int main() {
char name[5];
// Reads stream of 3 characters
cin.getline(name, 3);
// Print output
cout << name << endl;
return 0;
}
Input
GeeksOutput
GeExplanation
- cin.getline() reads an entire line of input, including spaces
- Reading stops when the specified character limit is reached
- It also stops when a newline character is encountered
cin.read()
It reads a stream of characters of given length N.
#include <iostream>
using namespace std;
int main() {
char gfg[20];
// Reads stream of characters
cin.read(gfg, 10);
// Print output
cout << gfg << endl;
return 0;
}
Input
Welcome to GFGOutput
Welcome toExplanation
- cin.read() reads a fixed number of characters from the input stream
- It does not stop when whitespace characters are encountered
- The specified number of characters are copied into the buffer
cin.ignore()
It ignores or clears one or more characters from the input buffer.
#include <iostream>
#include <ios>
#include <limits>
using namespace std;
int main() {
int x;
char str[80];
cout << "Enter a number and string:\n";
cin >> x;
// clear buffer before taking
// new line
cin.ignore(numeric_limits<streamsize>::max(), '\n');
// Input a string
cin.getline(str, 80);
cout << "You have entered:\n";
cout << x << endl;
cout << str << endl;
return 0;
}
Input
Enter a number and string:
8
Welcome to GFG
Output
You have entered:
8
Welcome to GFG
Explanation
- After reading an integer, the Enter key leaves a newline character in the input buffer
- If getline() is called immediately, it may read this newline character instead of actual input
- cin.ignore() removes the leftover newline character from the buffer
- This ensures that the subsequent getline() call works correctly
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. |