C++ is one of the most versatile languages in the world. It is used nearly everywhere for everything… systems programming (operating systems, device drivers, database engines, embedded, Internet of Things, etc.)

Enter Header Image Headline Here

Friday 14 April 2017

C++ cin

                                               C++ cin


In C++ Extraction operator is used to accept value from the user. User can able to acceptvalue from user and that value gets stored inside value container i.e. Variable.

Inmost program environments, the standard i/p by default is the kayboard, and the C++ stream object defined to access it is cin.

for formatted i/p operations, cin is used together with the extraction operator, which is written as >> (i,e,, two "greater than" signs). This operator is then followed by the variable where the extracted data is stored. For ex. -

1.  int age;
2.  cin >> age
  
Standard I/N in c++ is done through the use of streams. Streams are generic placse to send or receive data.  Keyword, screen, file, network, it's all the same after setup. It's a stream.

In c++, I/O is done through  classes and functions found in <iostream>.

there are two variables defined in <iostream>. cout is used for output, cin for input.


Important Point -

"cout and cin are not keywords in the c++ language. They are variables, instances of classes, that have been declared in <iostream>. "
 

Syntax : Get Value from User

cin >> variable ;

Explanation: Extraction Operator

  • Include <iostream.h> header file to use cin.

#include<iostream.h>//traditional c++

or

#include<iostream>// ANSI Standard

using namespace std;

  • cin is used for accepting data from the keyboard.
  • The operator >> called as extraction operator or get from operator.
  • The extraction operator can be overloaded.
  • Extraction operator is similar to the scanf () operation in C.
  • cin is the object of istream class.
  • Data flow direction is from i/p device to variable.
  • Due to the use of i/p statement, the program will wait till user type some i/p and that i/p is stored in variable.
  • Multiple inputs can be acceptsd using cin.


Live Example: Accepting i/p From user :


#include <iostream.h>
using namespace std;

int main()
{
    int number1;
    int number2;
 
    cout<<"Enter First Number: ";
    cin>>number1;                       //accept first number

    cout<<"Enter Second Number: ";
    cin>>number2;                       //accept Second number

   cout<<"Addition  : ";
   cout<< number1+ number2;                       //Display addition


   return 0;

}

Output : 

Enter First Number : 8
Enter Second Number : 8
Addition : 16















0 comments:

Post a Comment

Popular Posts

Recent Posts

Categories

Unordered List

Text Widget