C++ Input/Output

Introduction
C++ uses a convenient abstraction method called streams to perform input and output operations in sequential media such as the console (output stream) or via the keyboard (input stream). A stream is an object where a program can either insert or extract characters to or from it. We do not really need to care about many specifications about the physical media associated with the stream - we only need to know it will accept or provide characters sequentially.

The standard library of C++ includes the header file iostream, which contains functions that enable input / output of data. This is where the standard input and output stream objects are declared.

cin
It stands for 'console input. It is used together with the Extraction operator (>>) to extract data from the screen (console) into the program. The operator must be followed by the variable that will store the data that is to be extracted from the console.

The standard input device is the keyboard, cin can only process input from the keyboard when the return key is pressed.

Example: int score; cin>>score; The extraction operator may be used more than once in a statement for inputting values into different variables.

Example: int exam, cat1, cat2, cat3; cin>>cat1>>cat2>>cat3; //several extraction operators

cout
It stands for console input. It is used together with the insertion operator (<<) . The << is used to insert data that follows it into the stream preceding it. If a user wants to insert a constant string of characters into the output stream, then the characters are enclosed between double quotes. This is to make them clearly distinguished from variable names. Whatever is within the double quotes is displayed the way it is.

Example: cout<<"Welcome to C++ Programming";

To insert the contents of a variable into the output stream (the screen), simply specify the name of the variable.

Example: int score; cin>>score; cout<>cat1>>cat2>>cat3; cout<<"Cat 1: "<<cat1<<" Cat 2: "<<cat2<<" Cat 3: "<<cat3; //several insertion operators