Ads by ProfitSence

C++ cin

Last Updated on Wednesday 5th Oct 2022

cin c++

Learn about cin object in C++ with the help of examples.

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

  • cin is used together with the extraction operator, which is written as >> (i.e., two greater than signs).

c++ cin

					int age;
// take integer input
cin >> age;


cin in c++

cin Syntax

The syntax of the cin object is

					cin >> var_name;



  • >> is the extraction operator.
  • var_name is usually a variable, like arrays, vectors, lists, etc.

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

cin >>

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

					#include <iostream>
using namespace std;

int main() {
  int n1, n2, n3;
  cout << "Enter a number: ";
  // for single input
  cin >> n1;

  cout << "Enter 2 numbers:" << endl;

  // for multiple inputs
  cin >> n2 >> n3;

  cout << "Multiply of all = " << (n1 * n2 * n3);

  return 0;

					Enter a number: 1
Enter 2 numbers:
2 3
Multiply of all = 6


The cin object can also be used with other member functions such as getline() and read() etc.

  • cin.get(char &ch): Reads an input character and stores it in ch. cin.get() is used for accessing character array. It includes white space characters.
  • cin.getline(char *buffer, int length) Reads a stream of characters into the string.The C++ getline() is a standard library function that is used to read a string or a line from an input stream.
  • *buffer, int n): Reads n bytes (or until the end of the file) from the stream into the buffer.
					#include <iostream>
using namespace std;

int main() {
  char fName[20], lName[20];
  cout << "First Name: ";

  cin.getline(fName, 20);

  cout << "Last Name: ";

  cin.getline(lName, 20);

  cout << "You Entered : " << endl;
  cout << "Your Name = " << fName <<  lName<< endl;

  return 0;

					First Name: Mark
Last Name: Wheel
You Entered : 
Your Name = MarkWheel