Open In App

cin in C++

Last Updated : 29 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 Code
int 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 Code
int 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:

cin.getline(char *buffer, int N):

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 Code
int main()
{
    char name[5];
  
    // Reads stream of 3
    // characters
    cin.getline(name, 3);
  
    // Print output
    cout << name << endl;
  
    return 0;
}


Input:
 

 

Output:

 

cin.get(char& var):

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;
    cin.get(ch, 25);
  
    // Print ch
    cout << ch;
}


Input:

Output:

 

cin.read(char *buffer, int N):

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 Code
int main()
{
    char gfg[20];
  
    // Reads stream of characters
    cin.read(gfg, 10);
  
    // Print output
    cout << gfg << endl;
  
    return 0;
}


Input:

 

 

Output:

 

 

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>
  
// used to get stream size
#include <ios>
  
// used to get numeric limits
#include <limits>
using namespace std;
  
// Driver Code
int 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.



Previous Article
Next Article

Similar Reads

cin get() in C++ with Examples
cin.get() is used for accessing character array. It includes white space characters. Generally, cin with an extraction operator (&gt;&gt;) terminates when whitespace is found. However, cin.get() reads a string with the whitespace. Syntax: cin.get(string_name, size); Example 1: // C++ program to demonstrate cin.get() #include &lt;iostream&gt; using
1 min read
Problem with getline() after cin &gt;&gt;
The getline() function in C++ is used to read a string or a line from the input stream. The getline() function does not ignore leading white space characters. So special care should be taken care of about using getline() after cin because cin ignores white space characters and leaves it in the stream as garbage. Program 1: Below is the C++ program
2 min read
Cin-Cout vs Scanf-Printf
Regular competitive programmers face common challenge when input is large and the task of reading such an input from stdin might prove to be a bottleneck. Such problem is accompanied with “Warning: large I/O data”. Let us create a dummy input file containing a line with 16 bytes followed by a newline and having 1000000 such lines, making a file of
3 min read
Using return value of cin to take unknown number of inputs in C++
Consider a problem where we need to take an unknown number of integer inputs. A typical solution is to run a loop and stop when a user enters a particular value. How to do it if we are not allowed to use if-else, switch-case, and conditional statements? The idea is to use the fact that 'cin &gt;&gt; input' is false if the non-numeric value is given
2 min read
How to Redirect cin and cout to Files in C++?
In C++, we often ask for user input and read that input using the cin command and for displaying output on the screen we use the cout command. But we can also redirect the input and output of the cin and cout to the file stream of our choice. In this article, we will look at how to redirect the cin and cout to files in C++. Redirecting cin and cout
2 min read
How to Take std::cin Input with Spaces?
In C++, std::cin is used to accept the input from the standard input device. By default, the delimiter used by std::cin to separate input is a whitespace. In this article, we will learn how to take std::cin input with spaces in C++. Example: Input: Hey! Geek Welcome to GfG //input with spacesOutput: Input Entered: Hey! Geek Welcome to GfGstd::cin I
2 min read