Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
cin.get() in C++
3.
Use of cin.get()
4.
Versions of cin.get() in C++
4.1.
cin.get()
4.1.1.
Example
4.2.
C++
4.3.
cin.get(string_name, size)
4.3.1.
Example 
4.4.
C++
5.
Frequently Asked Questions
5.1.
What happens when the function cin.get() reaches the end of the input stream?
5.2.
What is the difference between cin and cin.get() in C++?
5.3.
Is it only possible to use cin.get() for reading characters from the keyboard?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

cin.get() in C++

Author Shiva
0 upvote

Introduction

User input plays a role in programming as it facilitates interaction between the program and the user. In C++, cin.get() is a function that enables programmers to obtain input from users making programs more dynamic and interactive.

cin.get() in C++

In this article, we will explore the functionality of cin.get() in C++, understand how it operates and provide examples to reinforce the concept.

cin.get() in C++

In C++, the cin object is commonly utilised for reading input from sources, such as the keyboard. An essential member function of cin is cin.get(), which allows you to retrieve characters from the input stream, including spaces and newline characters.

The versatility of cin.get() lies in its ability to read characters individually, giving you control over the input process compared to input functions. This feature proves useful when dealing with each character individually or addressing scenarios while processing user input.

Use of cin.get()

The cin.get() function in C++ lets us work with character arrays. Unlike cin(), which can't handle spaces, cin.get() includes spaces. Moreover, cin.get() allows us to set a character limit for input. Examples in the following section will clarify this concept further through code illustrations and explanations.

Versions of cin.get() in C++

The cin.get() in C++ can be specified in two ways. One with parameters and one without parameters.

cin.get()

The basic cin.get() function reads a single character from the input stream. The return value is of integer type. Here’s the syntax:

int cin.get();

 

When using this format, the cin.get() function retrieves the character from the input stream. Provides its ASCII value as a whole number. You can save this in an integer variable. If the input reaches its end, the function will give back a value known as the end of file (EOF) typically shown as the EOF.

Let’s understand this with an example. Suppose you have a program that asks the user to enter a single character. 

Example

  • C++

C++

#include <iostream>
using namespace std;
int main() {
   char ch;
   cout << "Enter a character: ";
   ch = cin.get(); 
   cout << "Entered character is : " << ch;
   return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 

Output:

output

 

Explanation: In this simple program, cin.get() reads a character from the input stream and stores it in character type variable ch. Then we display the character using cout.

cin.get(string_name, size)

This version takes two arguments, one for string name and the other for size. This allows you to read a sequence of characters. You can even read spaces, making it much more favourable than just cin. Also, you can read it only for a specified limit. Here's how it looks:

cin.get( array_name, array_size );

 

array_name holds the address of the array. Also, array_name holds the address of only the first element. The rest of the elements are traversed till encountered with EOF. When using cin.get(); you can input characters. Save them in the character array s until either n-1 characters have been entered or a newline character is encountered, whichever happens first. After that, the final position in the array is populated with a terminator ‘\0’.
 

Let’s take a look at an example. In this example, the cin.get() function reads a text, including spaces and stores it in an array.

Example 

  • C++

C++

#include <iostream>
using namespace std;

int main() {
   const int size = 100;
   char buffer[size];
   cout << "Enter a sentence: ";
   cin.get(buffer, size); 
   cout << "You entered: " << buffer <<endl;
   return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 

Output: 

output

 

Explanation: In this simple program, cin.get() reads a character from the input stream and stores it in character type variable ch. Then we display the character using cout.

Also see, Abstract Data Types in C++

Frequently Asked Questions

What happens when the function cin.get() reaches the end of the input stream?

When cin.get() reaches the end of the input stream it returns a value called end of file (EOF). This constant, known as EOF indicates that the input stream has reached its end.

What is the difference between cin and cin.get() in C++?

Both of them serve the same purpose. It differs in how they handle the input. The cin function reads input until it encounters whitespace characters such as spaces or newlines. On the hand cin.get() reads input, including whitespace characters. This makes cin.get() more suitable for reading lines of text that may contain spaces.

Is it only possible to use cin.get() for reading characters from the keyboard?

You can use cin.get() to read characters from any input stream, not limited to keyboard inputs. It is possible to redirect inputs from files or other sources, and cin.get() will function similarly.

Conclusion

cin.get() in C++ is a great alternative to the plain cin object for input. It makes reading data from the input stream much easier. By incorporating this function into your programs, you can create user-friendly applications irrespective of whether you're working on console applications or complex user interfaces.

Recommended Readings:

You may refer to our Guided Path on Code Ninjas Studios for enhancing your skill set on DSACompetitive ProgrammingSystem Design, etc. Check out essential interview questions, practice our available mock tests, look at the interview bundle for interview preparations, and so much more!

Happy Learning, Ninja!

Live masterclass