- We have used the following #include directive in the program #include <iostream>
- This directive causes the preprocessor to add the contents of the iostream file to the program.
- It contains declarations for the identifier cin , cout and the operator >> , <<
- The header file iostream should be included at the beginning of all programs that use input/output statements.
In C++ Input-Extraction Operator >> take value from keyboard and Output-Insertion Operator << put (display) value on screen
Input (Extraction ) Operator >> in C++
cin » variable-1 >> variable-2>> variable-n;
in C++ Output Operator also known as Extraction operator
- The program to wait for the user to type in a number. The number keyed in is placed in the variable-1 to n.
- The identifier cin (pronounced ‘C in’) is a predefined object in C++ that corresponds to the standard input stream. Here, this stream represents the keyboard.
- The operator » is known as extraction or get from It extracts <or takes) the value from the keyboard and assigns it to the variable on its right .
Output (Insertion ) Operator << in C++
cout <<“C++ is better than C”;
in C++ Output Operator also known as Insertion operator
- The statement causes the string in quotation marks to be displayed on the screen.
- This statement introduces two new C++ features, cout and «.
- The identifier cout is a predefined object that represents the standard output stream in C++. Here, the standard output stream represents the screen.
- It is also possible to redirect the output to other output devices.
- The operator « is called the insertion or put to
- It inserts (or sends) the contents of the variable on its right to the object on screen
float a,b,sum = 0 , avg ;
cout<<”Enter two Values \n”;
cin>> a >> b ;
sum = a + b;
avg = sum / 2 ;
cout<<”SUM = ”<< sum ;
cout<<”AVERAGE = ”<< avg;
Enter two Values
SUM = 21
AVERAGE = 10.50