Exforsys

Home arrow Technical Training arrow C++ Tutorials

C++ Standard Input Output Stream Page - 2

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

C++ Standard Input Output Stream

Standard Output Stream

A By default, the device used for output is the screen of the computer. For outputting values the keyword cout is used, which is an object. The insertion operator << is used on the standard output cout stream. The syntax for using the standard output stream is cout followed by the operator << followed by the value to be inserted or output by the insertion operator.

Ads

For example:

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


In the above example, the variable prog is declared as an integer type variable. The next statement is the cin statement that waits for input from the user"s keyboard. This information is then stored in the integer variable prog. The value of prog is displayed on the screen by the standard output stream cout. It is also possible to display a sentence as follows:

cout << "Training given by Exforsys ";

The above gives output as:

Training given by Exforsys

If a programmer chooses to use constant strings of characters, they must be enclosed between double quotes " " .

In this situation, it is important to note the difference between the two statements below:

Sample Code
  1. cout << "exforsys";
  2. cout << exforsys;
Copyright exforsys.com


In the above, the first statement displays on the screen as exforsys. The second statement outputs the value of the variable exforsys. As previously explained, the extraction operator >> can be used more than once in a single cin statement. Similarly, it is possible to use the insertion operator << more than once in a cout statement.

In this situation, it is important to note the difference between the two statements below:

For example:

Sample Code
  1. cout << "Exforsys " << "gives" << " excellent training";
Copyright exforsys.com


This produces output on the screen as:

Exforsys gives excellent training

The above concept is mainly used if the programmer chooses to print string constants followed by variables.

In this next example, the programmer chooses to display a combination of string constants and variables.

For example:

Sample Code
  1. int a=50;
  2. cout << "Exforsys has given " << a << " numbers of trainings";  
Copyright exforsys.com


This produces the output as:

Exforsys has given 50 numbers of trainings

Below is one more example:

Sample Code
  1. cout << "Exforsys";
  2. cout << "Training";
Copyright exforsys.com


The above produces output as:

Exforsys Training

An important point to note from the above example is cout does not give a line break unless specified. If the programmer chooses to display output in a new line, it must be explicitly specified in cout by using the n which denotes newline character.

For example:

Sample Code
  1. cout << "Exforsysn";
  2. cout << "Training";
Copyright exforsys.com


gives output as

Exforsys

Training

There is also another way for specifying newline that is by using endl manipulator (end line).

For example:

Sample Code
  1. cout << "Exforsys" << endl;
  2. cout << "Training";
Copyright exforsys.com


gives output as

Exforsys

Training

Ads
Example to demonstrate the use of input and output streams

Sample Code
  1. // Example to demonstrate the use of Input and Output streams
  2. #include <iostream>                    
  3. using namespace std;                           
  4.  
  5. void main()
  6. {
  7.    int a,b;
  8.    cout << "Enter the value of a: ";
  9.    cin >> a;
  10.    b=a+10;
  11.    cout << "Value of b is: " << b;
  12. }
Copyright exforsys.com


This produces the following output:



 
 

Comments