Read input in C++ | cin statement in C++ | get() function in C++ | getline() function in C++
There are different statements and functions to Display output in C++. They are as follows:
- cin
- get()
- getline()
1. cin statement in C++
We can use cin statement in C++ to read input from keyboard. It can be used to read value of any primary data type. like int, float, char etc.
cin is followed by >>(extraction operator ) further followed by the variable whose value we want to read.
We need to include header file <iostream> and import namespace std in our program to use cout.
Syntax:
cin>>var;
Here, var refers to the variable whose value should be entered from keyboard.
//Program to demonstrate the use of cin statement in C++. #include<iostream> using namespace std; int main() { int n; cout<<"Enter an integer value="; cin>>n; cout<<"\nn="<<n; return 0; }
Output
Enter an integer value=200 n=200
2. get() function in C++
We can use get() function in C++ with cin statement to read value of a character/string variable from keyboard. We can use it with input file stream object to read characters from a file.
We need to include header file <iostream> and import namespace std in our program to use cout.
Syntax:
cin.get(var,Limit,Termination);
Here, var is the character/string variable whose value we want to read .
Limit is the maximum number of characters which can be stored in the string variable.
Termination specifies the character that can be used to stop reading.
Example1:
char v; cin.get(v);
In the above Example:, cin.get() statement will only read one character at a time and will store it in character variable v.
Example2:
char n[2]; cin.get(n,30);
In the above Example:, cin.get() statement reads string variable n and stores maximum 29 characters in it.
Example:3:
char n[30]; cin.get(n,30,’$’);
In the above example cin.get() statement reads value of string variable n with maximum limit of 29 characters. Reading terminates on entering $ sign from keyboard. because $ sign is the termination character. It will read complete 29 characters if we don’t type terminating character $ while reading,.
//Program to demonstrate the use of get() function to read a character and a string variable.
#include<iostream> using namespace std; int main() { char ch; char str[30]; cout<<"\nEnter any character="; cin.get(ch); cout<<"\nEnter any string="; cin.get(str,30); cout<<"\ncharacter="<<ch; cout<<"\nString="<<str; return 0; }
Output
Enter any character=A Enter any string=Pathankot character=A; String=Pathankot
3. getline() function in C++
We can use getline() function in C++ with cin statement to read value of a character/string variable from keyboard. We can use it with input file stream object to read characters from a file. It also reads blank space which is not possible using simple cin statement.
We need to include header file <iostream> and import namespace std in our program to use cout.
Syntax:
cin.getline(var,Limit,Termination);
Here, var is the character/string variable whose value we want to read .
Limit is the maximum number of characters which can be stored in the string variable.
Termination specifies the character that can be used to stop reading.
Example1:
char v; cin.getline(v);
In the above Example:, cin.get() statement will only read one character at a time and will store it in character variable v.
Example2:
char n[2]; cin.getline(n,30);
In the above Example:, cin.get() statement reads string variable n and stores maximum 29 characters in it.
Example:3:
char n[30]; cin.getline(n,30,’$’);
In the above example cin.get() statement reads value of string variable n with maximum limit of 29 characters. Reading terminates on entering $ sign from keyboard. because $ sign is the termination character. It will read complete 29 characters if we don’t type terminating character $ while reading,.
//Program to demonstrate the use of getline() function to read a character and a string variable.
#include<iostream> using namespace std; int main() { char ch; char str[30]; cout<<"\nEnter any character="; cin.getline(ch); cout<<"\nEnter any string="; cin.getline(str,30); cout<<"\ncharacter="<<ch; cout<<"\nString="<<str; return 0; }
Output
Enter any character=K Enter any string=Ptk character=K String=Ptk