Simple user input in integer form – Computer Sir Ki Class

Login


Lost your password?

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

Login
[lwa]



Code Learning #CPP#1712 siteicon   siteicon   siteicon  

Simple user input in integer form

Learning the ways to take simple user input in integer form.

Learning Objectives

  • Collecting simple user input in an integer form.
  • use of cin and io extraction operator.

Program Approach

  • Create an space where the user input would be stored.
  • Prompt the user for an input using the cout.
  • Collect the user input using cin and >> (io extraction operator)
  • Display the input given by the user.

Source Code

TC++ #1712

 

Source Code

Run Output

Enter an integer number: 23
You have entered 23

or

Enter an integer number: 23.45
You have entered 23


Code Understanding

int n;
Here we declare a variable of our choice. In this case we have named it n. For this we decide to use the integer data type.

cout<<“Enter an integer number: “;
This line is used to prompt the user what has to be input.

cin>>n;
This is the line which collects the user input using the cin object in c++.. This object is followed by >> operator which is also called the “io extraction operator”. Some literature also name it as the “get from console” operator.

cout<<“You have entered “<<n<<endl;
This line is used to display the output as entered by the user. n is the variable which will be filled by the user when cin collects the required data.

Notes

In this code if user input is not an integer,say it is a fraction value like 23.45, then cin will still collect it. But! since the storage variable n is of integer type, thus the stored value will only be the integer portion of the input. So the output will only be 23.


Suggested Filename(s): simpleinput.cpp, intinput.cpp



Share

CSKC| Created: 14-Dec-2017 | Updated: 31-Aug-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