Exforsys.com
 
Home Tutorials C++
 

C++ Standard Input Output Stream

 
Category: C++
Comments (3)

C++ Standard Input Output Stream

Page 1 of 2

C++ Standard Input Output Stream

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


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.

 

Sponsored Links

 


What is a Stream?

A stream is an object where a program can either insert or extract characters to or from it. 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 overloaded 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:


int prog;
cin >> prog;


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 wait before proceeding for processing or storing the value. This duration is dependent on the user pressing the RETURN key on the keyboard. The input stream cin waits for the user to press the RETURN key then begins to process the command. 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:


cin >> x >> y;


is the same as:


cin >> x;
cin >> y;


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.



// This is a sample program         This is a comment Statement


#include <iostream.h>                  Header File Inclusion Statement
void main()
{
int sample, example;
cin >> sample;
cin >> example;
}



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 //.

 

Sponsored Links

 


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.


Next Page: Standard 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 - 2010 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape