cin in C++ (original) (raw)

Last Updated : 30 May, 2026

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.

#include 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,

**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

C++ `

#include 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

**Explanation

**Example: Taking Multiple Inputs Using the Extraction Operator(>>) with cin

C++ `

#include 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

**Explanation

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.

C++ `

#include 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

**Explanation

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).

C++ `

#include 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

**Explanation

cin.read()

It reads a stream of characters of given length **N.

C++ `

#include 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

**Explanation

cin.ignore()

It ignores or clears one or more characters from the input buffer.

C++ `

#include #include #include 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

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.