C++

How to Use cin.get() in C++

The cin.get() function in C++ is a system call function that you can use to read input data from the standard input or keyboard. This function reads characters from the input stream, which also includes white space characters (newlines and spaces). It’s a type of input function that reads the new character from the input stream no matter what type of character it is.

Methods of Using cin.get() in C++

There are two ways of using cin.get() function in C++:

1: Reading Single Character Using cin.get() in C++

In C++, you can read and save specific characters in the input buffer using the cin.get() method. It belongs to the “iostream” class as a member function. With the aid of whitespace and line breaks, the function can read a single character from the input stream and save it in a variable or an array.

The basic syntax for cin.get() is:

cin.get(char_variable)

This will take one character from the input stream, read it, save it, and then assign it to the char_variable. You can use it in combination with loops and arrays to read numerous characters.

#include <iostream>
using namespace std;

int main() {
    char name, name1, name2;
    cout << "Type Name: " << endl;
    cin.get(name);
    cin.get(name1);
    cin.get(name2);
    cout <<">"<< name<<"\n";
    cout <<">"<< name1<<"\n";
    cout <<">"<< name2<<"\n";
    return 0;
}

In the above example, we are creating three variables: name, name1 and name2 and getting input from the user. The cin.get() function is then used to take characters from the input stream one by one, and assign them to the variables. Only one variable is read by cin.get() function at one time.

Output

2: Reading Multiple Characters Using cin.get() in C++

You can also use the cin.get() function to read multiple characters from the input stream. This can be done by specifying the input buffer and the maximum number of characters to read from the input stream.

The basic syntax for cin.get() to read multiple characters in C++ is given below:

cin.getline(input buffer, max buffer size)

The characters read into the input stream buffer are stored in an array called input buffer, and the maximum number of characters that may be read before the read-in process terminates is indicated by max buffer size.

Here is an example of a simple code:

#include <iostream>
using namespace std;

int main() {
    char name[15];
    cout << "Type Name: " << endl;
    cin.get(name, 15);
    cout << name;
    return 0;
}

In the above code, we created a character-type variable called name. Additionally, the character array's size, which in this case is 15, was specified. The cout() function is then used to produce a message requesting the user to provide the name. The name was then obtained using the cin.get() method, and the length of the string was 15. Thus, the output will likewise display the string value with a size of 15 when we print the variable name as the output.

Output

Why Do We Need cin.get() in C++

The cin() function is often used to read input data from the keyboard or standard input. Yet when cin() is used to receive user input, it halts when it encounters a whitespace character, such as space, newline, or tab. The cin() function can thus cause problems when used in a code block that has to accept inputs containing whitespace.

This issue may be effectively solved by using the cin.get() method, which allows you to read input data up to a defined delimiter, including whitespace characters that the cin() function would ordinarily disregard. Any character, such as a newline or a particular character specified by the programmer, can serve as this delimiter.

Another crucial difference between cin() and cin.get() is that the latter does not toss out the newline character at the end of a user input line. Thus, it is strongly advised to read in data using cin.get() and the getline() method together.

There is a restriction with the cin.get() function: it can only read characters up to the newline character or the specified amount of characters, whichever comes first. As a result, if you try to read a line of text that is longer than the buffer allows, it will be truncated. Use the getline() method instead to prevent this.

Conclusion

The cin.get() function in C++ is used to read and save specific characters from the input stream. It can read single characters, and entire lines of text, and continue processing characters until the input is finished. Despite certain drawbacks, it is nevertheless an important feature for C++ programmers.

About the author

Hiba Shafqat

I am a Computer Science student and a committed technical writer by choice. It is a great pleasure to share my knowledge with the world in which I have academic expertise.