Exforsys

Home arrow Technical Training arrow C++ Tutorials

C++ Standard Input Output Stream

Page 1 of 2
Author: Sripriya R     Published on: 24th Aug 2007    |   Last Updated on: 25th Jul 2011

In this C++ tutorial, you will learn about standard input stream and standard output stream explained along with syntax and examples.

It is C++ programming language uses the concept of streams to perform input and output operations using the keyboard and to display information on the monitor of the computer.

What is a Stream?

A stream is the source or destination of a series of data, either characters, or in the case of binary files, a sequence of bytes that represent memory content. The standard input and output stream objects of C++ are declared in the header file iostream.

Standard Input Stream

Generally, the device used for input is the keyboard. For inputting, the keyword cin is used, which is an object. The operator of extraction, >>, is used on the standard input stream, in this case: cin stream. Syntax for using the standard input stream is cin followed by the operator >> followed by the variable that stores the data extracted from the stream.

For example:

Sample Code
  1. int prog;
  2. cin >> prog;
Copyright exforsys.com


In the example above, the variable prog is declared as an integer type variable. The next statement is the cin statement. The cin statement waits for input from the user"s keyboard that is then stored in the integer variable prog.

The input stream cin is waiting for the user to input a value,before proceeding for processing or storing the value. This duration is dependent on the user pressing the RETURN key on the keyboard. It is also possible to request input for more than one variable in a single input stream statement. A single cin statement is as follows:

Sample Code
  1. cin >> x >> y;
Copyright exforsys.com


is the same as:

Sample Code
  1. cin >> x;
  2. cin >> y;
Copyright exforsys.com


In both of the above cases, two values are input by the user, one value for the variable x and another value for the variable y.

Sample Code
  1. // This is a sample program        
  2. //This is a comment Statement
  3. //For using MS VS2005 and later, iostream requires the addition of a namespace
  4. //instead of <iostream.h>, just like in the following example
  5.  
  6. #include <iostream>                     //Header File Inclusion Statement                      
  7. using namespace std;                           
  8. void main()
  9. {
  10.         int sample, example;
  11.         cin >> sample;
  12.         cin >> example;
  13. }
Copyright exforsys.com


This produces the following output:

If a programmer wants to write comments in C++ program, the comments should follow after a pair of slashes denoted by //. All the characters after the // are ignored by C++ compiler and the programmer can choose to comment after the //.

Ads

In the above example, two integer variables are input with values. The programmer can produce input of any data type. It is also possible to input strings in C++ program using cin. This is performed using the same procedures. The vital point to note is cin stops when it encounters a blank space. When using a cin, it is possible to produce only one word. If a user wants to input a sentence, then the above approach would be tiresome. For this purpose, there is a function in C++ called getline.



 
 

Comments