Input of different data types using cin
Learning to input different data types using the standard console in object.
Learning Objectives
- Usage of cin for different datatypes.
- Multiple cin usage successively.
- Forming cout chain with different data types.
Source Code
|
Run Output
Code Understanding
char c; int n; float f;
Here we are declaring three different variables of different data types.
cout<<“Enter a single character: “; cin>>c;
Here we are collecting a char type input data. This is because the storage type of variable is char.
cout<<“Enter an integer: “; cin>>n;
Here we are collecting a integer type input data. This is because the storage type of variable is int.
cout<<“Enter a float number: “; cin>>f;
Here we are collecting a float type input data. This is because the storage type of variable is float.
cout<<endl<<“You entered ” <<c<<“, “<<n<<“, “<<f<<endl;
Here we are displaying all different types of data using the same cout chain formation.
Notes
- cin can be used with different datatypes automatically without specifying the datatypes. This is an example of polymorphism or overloading of a method. In the same way you can also see polymorphic behaviour of cout also which can output different data types in the same formation.
Suggested Filename(s): inputmany.cpp, cin-char-int-float.cpp
CSKC| Created: 15-Dec-2017 | Updated: 5-Sep-2018|