Exforsys.com
 

Sponsored Links

 

C++ Tutorials

 
Home Tutorials C++
 

C++ Standard Input Output Stream

 
Category: C++
Comments (3)

Standard Output Stream

Page 2 of 2


Standard Output Stream

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.



For example:


int prog;
cin >> prog;
cout << prog;


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:


cout << "exforsys";
cout << exforsys;


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.


For example


cout << "Exforsys" << "gives" << "excellent training";


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



int a=50;
cout << "Exforsys has given" << a << "numbers of trainings";



This produces the output as:


Exforsys has given 50 numbers of trainings


Below is one more example:



cout << “Exforsys”;
cout << “Training”;



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


cout << "Exforsys\n ";
cout << "Training";


gives output as


Exforsys
Training


There is also another way for specifying newline that is by using endl manipulator.


For example


cout << "Exforsys" << endl;
cout << "Training";


gives the same output as


Exforsys
Training



Example to demonstrate the use of input and output streams



// Example to demonstrate the use of Input and Output streams
#include <iostream.h>
void main()
{
   int a,b;
   cout << “Enter the value of a:”;
   cin >> a;
   b=a+10;
   cout << “Value of b is:”<<b;
}





First Page: C++ Standard Input Output Stream


Read Next: C++ Operators Part I



 

 

Comments


Shafi.shaik said:

  this meterial is very easy to learning students. and also effected notes for faculty to improve your knowledge throgu this meterial
form sk.shafi
lecturer in private degree college in guntu(dt) A.P
October 21, 2008, 11:54 pm

geeta s h said:

  How are floating point values stored in memory?
April 24, 2009, 5:48 am

Sheece Gardezi said:

  first declace the variable in which the value is to be stored as float the simply use cin command

#include <iostream.h> Header File Inclusion
using namespace std;
main()
{
float decimal_number; //declaring variable as floatng point value!
int integer_number; //declareding variabe as simple integer!

cin >> decimal_number; //prompting for input

integer_number=decimal_number /*checking that if our variable was ..............................//not declared a floating point ..............................//variable then what value would ...............................//have been stored in memory*/

cout>> decimal_number; //output as float no
cout>> number; //output as integer

return 0;

}
November 6, 2009, 3:09 pm

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2009 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape