In C++, cin is an object of istream class that is used to accept the input from the standard input stream i.e. stdin which is by default associated with keyboard. The extraction operator (>>) is used along with cin to extract the data from the object and insert it to the given variable.
Let’s take a look at an example:
C++
#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
10
Output
10
Syntax of cin
cin >> var_name;
Here,
- >>: 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.
Understanding how to use cin
for input is fundamental in C++. To master input handling in your programs, consider exploring the C++ Course, which provides detailed explanations and practical examples.
Examples of cin
The following examples demonstrate how to use cin in C++ programs:
Taking a Text from User Input
C++
#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 GeeksforGeeks
Output
Welcome
Taking Multiple Inputs Using the Extraction Operator(>>) with cin
C++
#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 13
Output
Name : ABC
Age : 13
cin
Member Functions in C++
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 . |
---|
cin.get()
It reads an input character and stores it in a variable. Below is the C++ program to implement cin.get():
C++
// C++ program to illustrate the use
// of cin.get()
#include <iostream>
using namespace std;
// Driver Code
int main()
{
char ch[30];
cin.get(ch, 25);
// Print ch
cout << ch;
}
Input
Welcome to GFG
Output
Welcome to GFG
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). Below is the C++ program to implement cin.getline():
C++
// C++ program to illustrate the use of cin.getline
#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
Geeks
Output
Ge
cin.read()
It reads a stream of characters of given length N. Below is the C++ program to implement cin.read():
C++
// C++ program to illustrate the use of cin.read()
#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 GFG
Output
Welcome to
cin.ignore()
It ignores or clears one or more characters from the input buffer. Below is the C++ program to implement cin.ignore():
C++
// C++ program to illustrate the use of cin.ignore()
#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: In the above program if cin.ignore() has not been used then after entering the number when the user presses the enter to input the string, the output will be only the number entered. The program will not take the string input. To avoid this problem cin.ignore() is used, this will ignore the newline character.