C++ cin

The cin object is used to accept input from the standard input device i.e. keyboard. It is defined in the iostream header file.

Example

#include <iostream>
using namespace std;

int main() {
  int num;

  cout << "Enter a number: ";

// take integer input cin >> num;
cout << "You entered: " << num; return 0; }

Output

Enter a number: 25
You entered: 25

cin Syntax

The syntax of the cin object is:

cin >> var_name;

Here,

  • >> is the extraction operator.
  • var_name is usually a variable, but can also be an element of containers like arrays, vectors, lists, etc.

cin with Extraction Operator

The "c" in cin refers to "character" and "in" means "input". Hence cin means "character input".

The cin object is used along with the extraction operator >> in order to receive a stream of characters. For example,

int num;
cin >> num;

The >> operator can also be used more than once in the same statement to accept multiple inputs:

cin >> var1 >> var2 >> … >> varN;

Example 1: cin with Extraction Operator

#include <iostream>
using namespace std;

int main() {
  int num1, num2, num3;

  cout << "Enter a number: ";

// for single input cin >> num1;
cout << "Enter 2 numbers:" << endl;
// for multiple inputs cin >> num2 >> num3;
cout << "Sum = " << (num1 + num2 + num3); return 0; }

Output

Enter a number: 9
Enter 2 numbers:
1
5
Sum = 15

cin with Member Functions

The cin object can also be used with other member functions such as getline(), read(), etc. Some of the commonly used member functions are:

  • cin.get(char &ch): Reads an input character and stores it in ch.
  • cin.getline(char *buffer, int length): Reads a stream of characters into the string buffer, It stops when
    • it has read length-1 characters or
    • when it finds an end-of-line character '\n' or the end of the file eof.
  • cin.read(char *buffer, int n): Reads n bytes (or until the end of the file) from the stream into the buffer.
  • cin.ignore(int n): Ignores the next n characters from the input stream.
  • cin.eof(): Returns a non-zero value if the end of file (eof) is reached.

Example 2: cin with Member Functions

#include <iostream>
using namespace std;

int main() {
  char name[20], address[20];

  cout << "Name: ";

// use cin with getline() cin.getline(name, 20);
cout << "Address: ";
cin.getline(address, 20);
cout << endl << "You entered " << endl; cout << "Name = " << name << endl; cout << "Address = " << address; return 0; }

Output

Name: Sherlock Holmes
Address: Baker Street, UK

You entered
Name = Sherlock Holmes
Address = Baker Street, UK

cin Prototype

The prototype of cin as defined in the iostream header file is:

extern istream cin;

The cin object in C++ is an object of class istream. It is associated with the standard C input stream stdin.

The cin object is ensured to be initialized during or before the first time an object of type ios_base::Init is constructed.

After the cin object is constructed, cin.tie() returns &cout. This means that any formatted input operation on cin forces a call to cout.flush() if any characters are pending for output.


Also Read:

Did you find this article helpful?