The predefined object cin is an instance of istream class that accepts the user input. The cin object is said to be attached to the standard input device, which usually is the keyboard. The cin object is used in conjunction with the stream extraction operator (>>) that extracts the data from the input stream and stores the extracted data in a variable.
The syntax for using the cin object with a single variable and multiple variables is given below −
// For single variable cin >> variable_name;or// For multiple variables cin >> variable1 >> variable2 >>...>> variableN; where,>> is the extraction operator. variable_name, variable1, variable2,..., variableN are the variable names where we store the input values.
Read this chapter to get a good understanding of how the cin object works. We have used plenty of examples to explain all of its features in detail.
Taking User Input with cin
The following example demonstrates how to take a single integer and multiple integers as input using cin to calculate the sum of the numbers −
#include <iostream>usingnamespace std;intmain(){int num1;
cout <<"Enter number num1: ";
cin >> num1;
cout <<"Num1: "<< num1 << endl;int num2, num3;
cout <<"Enter the numbers num2 and num3: ";
cin >> num2 >> num3;
cout <<"Num2: "<< num2 <<", Num3: "<< num3
<<"\n"<<"Sum: "<< num1 + num2 + num3 << endl;return0;}
The output of the above code is as follows −
Enter number num1: 2 Num1: 2 Enter the numbers num2 and num3: 2 5 Num2: 2, Num3: 5 Sum: 9
Taking Array Input with cin
This example demonstrated how to take an array as input using cin:
#include <iostream>usingnamespace std;intmain(){int arr[5];int n =5;
cout <<"Enter "<< n <<" array elements: ";for(int i =0; i < n; i++){// Taking array elements input
cin >> arr[i];}
cout <<"Array elements: ";for(int i =0; i < n; i++){
cout << arr[i]<<" ";}return0;}
The output of the above code is as follows −
Enter 5 array elements: 1 2 3 4 5 Array elements: 1 2 3 4 5
C++ cin Object Functions
Below is a table of most commonly used functions of C++ cin object −
| Functions | Definition |
|---|---|
| cin.get() | The cin.get() function reads a single character including whitespace from input stream. |
| cin.getline() | It reads a line of text from user input along with whitespaces until it reaches end of the line or newline character. |
| cin.read() | You can specify the number of characters you want to read from input stream using cin.read() function. |
| cin.putback() | The cin.putback() function puts a character back into the input stream that was removed after using get() function to read the character. |
| cin.peek() | It looks at the next character in the input stream without removing it unlike get() function that removes the character. |
| cin.good() | It checks the state of input stream and returns true if the input stream is in a good state with no errors. |
| cin.bad() | It checks the state of input stream and returns true if an error occurs in the stream that can not be recovered. |
| cin.fail() | It returns true for failed operations on the input stream. |
| cin.clear() | It clears the error flags of the stream and reset it to good state. |
| cin.ignore() | The cin.ignore() function is used to skip and discard characters from the input buffer so they are not read by the next input operation. |
| cin.gcount() | It returns the count of characters extracted by the last unformatted input operation including whitespaces. |
| cin.seekg() | It is used in file handling to set the position of the get pointer in the input stream. It is generally used with file streams (ifstream). |
| cin.eof() | It returns true upon reaching end of file in the input stream. |
| cin.rdbuf() | It returns the stream buffer object of cin that can be used to read from or redirect the input stream directly. |
Here are the example codes of each member function listed above in the table −
get() getline() read() putback() peek()
In this example, we have used the cin.get() function to read the first character of input stream −
#include <iostream>usingnamespace std;intmain(){
cout <<"Enter any character: ";char ch = cin.get();
cout <<"Entered character: '"<< ch <<"'"<< endl;char ch2 = cin.get();
cout <<"Entered character: '"<< ch2 <<"'"<< endl;return0;}
The output of the above code is given below. Here we have used the get() function twice. The first time it return the character ‘a’ and second time it returns a blank space.
Enter any character: a b c Entered character: 'a' Entered character: ' '
good() bad() fail() clear() ignore()
In this example, we are validating the user input for an integer. For integer input, it prints the integer and for non-integer inputs, it prints a message of invalid input.
#include <iostream>usingnamespace std;intmain(){int num;
cout <<"Enter an integer: ";
cin >> num;if(cin.good()){
cout <<"Valid input. Given number: "<< num << endl;}else{
cout <<"Invalid input. Input number is not an integer"<< endl;}return0;}
The output of the above code is as follows −
Enter an integer: 345 Valid input. Given number: 345 Enter an integer: abcd Invalid input. Input number is not an integer
Here is an example of cin.gcount() function returning the count of input characters including the whitespaces −
#include <iostream>usingnamespace std;intmain(){char buffer[100];
cout <<"Enter text: ";
cin.getline(buffer,100);
cout <<"Entered Text: "<< buffer << endl;
cout <<"Count of characters: "<< cin.gcount()<< endl;return0;}
The output of the above code is as follows:
Enter text: Tutorials Point Entered Text: Tutorials Point Count of characters: 16
Conclusion
In this chapter, we understood the cin object and its usage along with its member functions and their respective example codes.
Leave a Reply