cin.get() in C++

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

The cin.get() C++ function is used to access the character array. This is a basic function in the C++ programming language used to get user input. The cin.get() C++ function also includes the white space characters into it.

Use of cin.get()

As we know, the cin.get() C++ function is used to access the character array. What makes it different from the cin() function is that the cin function can not take the white spaces. But the cin.get() C++ function can include the white space character into them. Also, using the cin.get() C++ function, we can define and limit the number of characters the variable needs to access. Refer to the examples section to understand this in a better way with code and explanations of them.

To learn more about the cin function in C++, refer below link

Two Versions of cin.get() in C++

The cin.get() function in C++ can be used in two different ways. The first one is with specifying any parameters for the function. And the second one is by specifying the parameters of the function. Lets us discuss them below.

cin.get()

When we write the function as cin.get(), we are not specifying any condition or parameter for the function. If done so, the parameters should be seen inside the parenthesis.

cin.get(string_name, size)

When we write the function as cin.get(string name, size), which means we specify some condition or parameter inside the parenthesis for the function. The string name represents the name of the address of the string. And the string size is the integer value representing the number of characters we want to show as the output ( including white space ) from the string name.

Parameters of cin.get() in C++

The cin.get() in the C++ function takes two arguments. The first one is array name and the second one is array size.

The array_name is the address of the string. The name of the given array represents the address of the first element. And the array_size is the number of characters that can be stored in the array name. The array name is of char type, and the array size is of int type.

Return Value of cin.get() in C++

The return value of the cin.get() in the C++ function is the first string from the user input. The function waits until the user enters the input. And after pressing the enter key, the function returns the first string from the user-provided input.

Example:

Output:

Explanation: In the above example, the user provided their full name as the input. After that, we declared the number of characters in cin.get() function, which we need to get as the output. And the function returns the 5 characters (1 white space character and 4 characters from user input) as the output.

Examples

Now let us see some examples of the cin.get() in the c++ function to get a better understanding of this function.

Example 1

Output:

Explanation: In the above example, we created a variable of character type, namely name. And we also specified the size of the character array, which is 15 here. After that, we use the cout function to print a message asking the user to enter the name as the input. Then we used the cin.get() function to get the name, and the string size is 15. So, when we print the variable name as the output, the output will also show the string value of size 15.

Example 2

Output:

Explanation: In the above example, first, we created a variable of character type, namely name. We also defined the size of the characters the variable can take, which is 100. After that, we used the cout function to print a message asking the user to type a name for the input value. Then we used the cin.get() function to access the input provided by the user as variable name. But here, the string size is 4.

So, the cin.get() function will only take the four characters (including the white space character) from the string name and provide this as the output. There is a space after the << extractor in the cout << name function. Hence, the resultant output will be a space+three characters from the string name. That is joh in this example.

How Does cin get() in C++ Work?

The cin.get() in C++ is used for accessing the character array from the user that the user provides as the input. When we expand the cin.get() function, the c stands for "character" and the in stands for "input". And the get() here means that the function will also take the whitespace along with the input. But that does not happen in the cin function.

Now let us talk about how the cin.get() in c++ works. To use the cin.get() in C++ function inside the main code, first, we must create a variable from which the function will access the characters. Inside the cin.get() function, we declare two parameters. The first is the name of the array, and the second is the array size we want to get as the output. One thing to remember is that our array size also includes the white space characters inside the output. This way, the function provides the number of characters from the variable as the output. Refer to the example section for more details.

Conclusion

  • The cin.get() C++ function accesses the character array and includes the white space character inside the output.
  • Expanding the cin.get() function, the c stands for "character," and the in stands for "input," and the get() here means that the function will also take the whitespace along with the input.
  • The syntax of the cin.get() function is cin.get(string_name, size).
  • The C++ function cin.get() has two parameters: the string name and the string size.
  • The string name represents the name of the string's address. The string size is the integer value representing the number of characters we want to show as the output ( including white space ) from the string name.