Showing posts with label c user input. Show all posts
Showing posts with label c user input. Show all posts

Saturday 5 October 2013

Taking Input In C ++

Input in C ++:

 Taking input in C ++ programming is very important , as we learnt in previous C tutorial for beginners that C++ program is used to perform calculations and to manipulate data. And data can be manipulated after storing it in memory. In this C++ tutorial, we are going to learn that how to input (cin in c ++) data in computer’s memory.

Storing data in the computer’s memory is a two-step process:

  • Instruct the computer to allocate memory
  • Include statements in the program to put data into the allocated memory


There are two ways to store data:

  • Using cin operator ( >> )
  • Initializing the value in variables

Using cin Operator In C++:

Putting data into variables from the standard input device is accomplished via the use of "cin" and the operator ">>". The syntax of cin together with >> is as follows.

Syntax:

cin >> variable >> variable;
cin is used to take input in c ++ called an input (read) statement. In C++, >> is called the stream extraction operator. 

Suppose we have an int variable ‘number’.

And we take input:


cin >> number;
If user enters 30 then 30 is stored in the variable ‘number’.

For taking input of two variables:


Int feet;
Int inches;
\\  Suppose the input is:
\\  23 , 7
We’ll take input by writing
cin >> feet >> inches;

Declaring and initializing variables:

When we declare a variable at that instant there is a junk value in it. By default, C++ puts junk value in it. But when a variable is declared, memory is allocated according to the size of its data type.  So, when we assign the value in that variable for a first time then it’s called initialization. A variable is said to be initialized the first time a value is placed in the variable. An operator “ = ” is used to initialize the value. “ = ” is called the assignment operator. 

For example:

Int num1 = 20;   //we initialized it with the value the time we declared the variable
Int num2; // declaring variable num2
num2 = 4; //we initialized num2 after declaring it


Allocating memory with constant variables:

If the user want to initialize a variable and doesn’t want to get the value of that variable changed then C++ has given us the facility of using constant variables.

Syntax:

const <datatype> <identifier> = value;
In C++, const is a reserved keyword.
It is like a memory location whose content is not allowed to change during program execution.

Final words:

This c tutorial for beginners prepared by shapes in c will enable our viewers to learn how to take input in C ++.
cin in c++ is a very important keyword which has been been discussed in detail in this C++ tutorial.
A part from that we also discussed the differences between declaration and initialization.Thorough study of this article will definitely help you a lot.