Input of different data types using cin – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Code Learning #CPP#1719 siteicon   siteicon   siteicon  

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

TC++ #1719

 

Source Code

Run Output

Enter a single character: R
Enter an integer: 123
Enter a float number: 345.56

You entered R, 123, 345.56

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



Share

CSKC| Created: 15-Dec-2017 | Updated: 5-Sep-2018|






×
Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning 2D Array Memory Addressing Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Multi-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues


Back