cin in C++ - GeeksforGeeks

29 Jul, 2021
The cin object in C++ is an object of class iostream. It is used to accept the input from the standard input device i.e. keyboard. It is associated with the standard C input stream stdin. The extraction operator(>>) is used along with the object cin for reading inputs. The extraction operator extracts the data from the object cin which is entered using the keyboard.
Program 1:
Below is the C++ program to implement cin object:
C++
// C++ program to demonstrate the// cin object#include <iostream>using namespace std;  // Driver Codeint main(){    string s;      // Take input using cin    cin >> s;      // Print output    cout << s;      return 0;}
  
Input: 
 
Output:
Program 2:
Multiple inputs using the extraction operators(>>) with cin. Below is the C++ program to take multiple user inputs: 
C++
// C++ program to illustrate the take// multiple input#include <iostream>using namespace std;  // Driver Codeint main(){    string name;    int age;      // Take multiple input using cin    cin >> name >> age;      // Print output    cout << "Name : " << name << endl;    cout << "Age : " << age << endl;      return 0;}
  
Input:
 
Output:
 The cin can also be used with some member functions which are as follows:
It reads a stream of characters of 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;  // Driver Codeint main(){    char name[5];      // Reads stream of 3    // characters    cin.getline(name, 3);      // Print output    cout << name << endl;      return 0;}
Input: 
 
Output:
 
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 Codeint main(){    char ch;    cin.get(ch, 25);      // Print ch    cout << ch;}
Input:
Output:
 
Reads a stream of characters of 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;  // Driver Codeint main(){    char gfg[20];      // Reads stream of characters    cin.read(gfg, 10);      // Print output    cout << gfg << endl;      return 0;}
Input:
 
 
Output:
 
 
 
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>  // used to get stream size#include <ios>  // used to get numeric limits#include <limits>using namespace std;  // Driver Codeint main(){    int x;    char str[80];    cout << "Enter a number andstring:\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: 
 
Output:
 
 
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.
santhoshsandy0144
cpp-input-output
Input and Output
Input Output Systems
Picked
C++
C++ Programs
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Operator Overloading in C++
Polymorphism in C++
Sorting a vector in C++
Friend class and function in C++
Pair in C++ Standard Template Library (STL)
Header files in C/C++ and its uses
How to return multiple values from a function in C or C++?
C++ Program for QuickSort
Program to print ASCII Value of a character
Sorting a Map by value in C++ STL