How to use cin.get(), cin.peek() and cin.putback() in c++ programming.

Naman Tamrakar
4 min readJun 9, 2020

--

First of all, we have to understand how we used to take input while working on a console-based app. We use cin>>var to take some var variable into our program. The variable can be of int, double, char or string type. But the question is how this input works?

#include<iostream>
 using namespace std;
 
 int main(){
 char var;
 cin>>var;
 return 0;
 }

So when we give input on running this program and suppose we give input as cplusplus then this input is stored in input buffer stream like this and pointer(arrow) is placed at the first buffer.

Note:- This is not the pointer which holds the address.

values input buffer stream

Since we are using char type variable so the first value is stored in the variable var as ‘c’ and the pointer move to the next input buffer i.e. ‘p’.

Now if we are using another cin statement in our program may be for a variable var1 of char data type then the next value in the input buffer stream is assigned to the variable var1.

Using cin.get( ) instead of cin -

Now let’s talk about cin.get() which is almost same as cin>>var but the main difference in the two is that when we use cin>>var then it skips whitespace (space, newline, tab, etc.) but cin.get() don’t do so. I take whitespace into consideration. If we enter spaces in the input then it considers it as variable and assigns a null value to the variable. The syntax of cin.get() is to put the variable inside parentheses to which the value is to be assign from the input buffer stream. An example is given below-

Note:- cin.get() can only be used with char data type.

source code

Using cin.peek( ) and cin.putback( ) -

cin.peek() is same as cin.get() but it does not take out the value from the buffer stream instead it makes a copy of the value from the buffer stream value and assign it to the variable and also the pointer does not move forward to next buffer in the input buffer stream. One notable thing is that the syntax of cin.peek() is slightly different from the syntax of cin.get(). Instead of passing a variable inside the parentheses, we use to assign cin.peek() to the variable i.e var = cin.peek() to which we want to assign the input value. The example below illustrate the working of cin.peek().

Example source code.

When we enter the input cplusplus into the console then an input buffer stream is created as we have already discussed above. When we use var=cin.peek() then a copy of the value in the first buffer is assigned to var and the pointer also does not move forward and then we use cin.get(var1) so the value in the buffer on which the pointer is assigned to variable var1 and the pointer is moved forward to next buffer because this time we used here cin.get() which takeout value from the buffer.

The concept of cin.putback() is completely different from cin.peek() and cin.get(). The understanding of this concept can bring a drastic change in your c++ program and can save some of your time and some lines of code. So the function of cin.putback() is clear from its name itself. It put the values back into the buffer and move the pointer backwards. If we talk about the syntax so it is similar to the cin.get(). We just assign the value inside the parentheses to which we have to put back into the buffer. Below gives the illustration of how it works.

Example source code

After execution of cin.putback(‘d’);

So This was all about using cin.get(), gin.peek() and cin.putback() in your program which can make you c++ code more readable and simple and avoid you from using some self-generated function to do these silly tasks and make your code more drastic.

Thank you for reading this post. If you found this post helpful please give claps to this post and share with your friend to let them also know this functionality.

--

--

Naman Tamrakar

Hey 👋, I am a passionate engineering student trying to learn new things daily. Whatever I learn from multiple source I want to share it with other on medium.