INPUT AND OUTPUT OPERATORS in C++

 Introduction
              The input output operations are done using library functions cin and cout objects of the class iostream. Using the standard input and output library, we will able to interact with the user by printing message on the screen and getting the user’s input from the keyboard.
 A stream is an object where a program can either insert/extract characters to/from it. The standard C++ library includes the header file iostream, where the standard input and output stream objects are declared.

 Input Operator “>>”:
 The standard input device is usually the keyboard. Input in C++ is done by using the “stream extraction” (>>) on the cin stream. The operator must be followed by the variable that will store the data that is going to be extracted from the stream.

Example
int age;
cin>>age;
 The first statement declares a variable of the type int called age, and the second one waits for an input from cin (the keyboard) in order to store it in this integer variable.  cin stands for “console input”. It can only process the input from the keyboard once the RETURN key has been pressed. We must always consider the type of the variable that we are using as a container with cin extraction. For example, if we request an integer we will get an integer.


 Output Operator “<<”:
          The standard output device is the screen (Monitor). Outputting in C++ is done by using the object followed by the “stream insertion” (<<). cout stands for console output.
Example
cout<<”Let us learn C++”; // prints Let us learn C++ on the screen. The << operator inserts the data that follows it into the stream preceding it. The sentence in the instruction is enclosed between double quotes ( ” ), because it is constant string of characters.

cout<<”sum”; //prints sum
cout<<sum; //prints the content of the variable sum
 In order to perform a line break on the output we must explicitly insert a new-line character into cout. In C++ a new-line character can be specified as ‘\n’ (backslash n), the new-line character is an escape sequence character and helps in formatting the output statement.

Program: To demonstrate the cout statement

#include<iostream.h>
#include<conio.h>
void main( )
INPUT AND OUTPUT OPERATORS in C++ INPUT AND OUTPUT OPERATORS in C++ Reviewed by Unknown on 04:23 Rating: 5

No comments:

Powered by Blogger.