input by cin in c++

Cin in CPP

In this post, we shall learn about how to take input with the help of in the cin and uses it in the CPP language.

Taking input in cpp is not a difficult task. The object which is used for taking input in cpp is the cin.
The library which is allowed to use the cin object is the iostream.

Method for including the iostream library in the program.
The syntax is here:
#include<iostream>

How to take input in c++?

CIN is used for taking any kind of data from the user. The data may be int, float, and char.cin is also used to taking string in the CPP.

If someone learns CPP then cin is important to use because it allows to user to interact with your program.
The program will be user friendly and generic by using a cin object.

Data types

How to take input character?

#include<iostream>
using namespace std;
int main()
{
char ch;                        //declare variable of character
cout<<" enter the any character"<<endl;                //message for taking input
cin>>ch;                    //take input 


//display enter character
cout<<ch;


return 0;

}


In the above code, take input from the user as a character then display which character user input.
First, declare character variable taking saving input in the variable.we can not save character input in the other type of data type.
Sometimes need to take input as a character, in this scenario above code is very helpful.

How to take input  integer?

#include<iostream>
using namespace std;
int main()
{
int integar1;
int integar2;
cout<<" enter the first number "<<endl;//print message for input 
cin>>integar1;//take input ist number

cout<<" enter the 2nd number "<<endl;//message for 2nd number
cin>>integar2;//take input for 2nd number

//display numbers
cout<<integar1<<endl;
cout<<integar2;

return 0;

}


Output:

enter the first number 2
enter the 2nd number 4
2
4

In the above example, take input with the help of CIN from the user as an integer. First, declare an integer variable to save the input data. Then accept input in the integer variable through cin. Take care to write the extraction(>>) operator after the CIN keyword.

How to take input float numbers?

#include<iostream>
using namespace std;
int main()
{
float float1;
float float2;
cout<<" enter the first number "<<endl;
cin>>float1;

cout<<" enter the 2nd number "<<endl;
cin>>float2;

//display entered numbers
cout<<float1<<endl;
cout<<float2;

return 0;

}


cin cpp



In the above code, take input as a float from the user with the help of CIN statement.we can also take input as a float number with the help of a double data type. Double give more precision as compared to the float. Take care before taking the input used a variable for saving input must be float or double.If other data types used then some data loss due to data truncate.

In the above examples, cout and cin both are used if you do not know about cout then visit cout.
I think from the above example you have enough idea how to use the cin in cpp. If any problem then
comment in the comment section.


Comments

Popular posts from this blog

Diferrence between flutter and react native

Flutter Widgets

Flutter layout