codescracker


c++

C++ Input Output Stream



« Previous Tutorial Next Page »


In C++, all devices are treated as files. Thus, the standard input device (the keyboard), the standard output device (the screen or monitor), and the standard error device (the screen or monitor) (where the errors, if any, are displayed) are all treated as files. At its lowest level, a file is interpreted simply as a sequence, or stream of bytes. At this level, the notion of a data type is absent i.e., data is treated simply as sequence of bytes without considering its data type. However, at the user level, a file consists of a sequence of possibly intermixed data types-characters, arithmetic values, class objects etc.

Function of iostream.h

There are the following functions of iostream.h at different level :

  1. At the lowest implementation level, where the notion of data type is missing and files are treated as streams of bytes, the I/O library manages the transfer of these bytes.
  2. At the user level, where the notion of data types is present, I/O library manages the interface between these two levels i.e., between user level and the lowest implementation level.
  3. The I/O library predefines a set of operations for handling reading and writing of build-in data types.

Predefined Streams in I/O Library

As already mentioned, at lowest levels, files are implemented as streams of bytes. A Stream is simply a sequence of bytes. Input and Output operations are supported by the istream (input stream) and ostream (output stream) classes.

The ostream output operation, referred to as insertion is performed by the insertion operator "<<". The istream input operation, referred to as extraction, is performed by the extraction operator ">>".

Predefined Stream Objects

The predefined stream objects for the input, output and the error are as follows :

  1. cin (pronounced "see-in"), as istream class object tied to standard input. cin stands for console input.
  2. cout (pronounced "see-out"), as ostream class object tied to standard output. cout stands for console output.
  3. cerr (pronounced "see-err"), as ostream class object tied to standard error. cerr stands for console error.

Let's discuss the examples of all the above three predefined stream objects for input, output and error :

C++ standard output stream

Here is an example on standard output stream in C++

/* C++ Input and Output Stream */

#include<iostream.h>
#include<conio.h>

void main()
{
	clrscr();
	char str[] = "Welcome to codescracker.com\nThis is C++ Tutorial.";
	cout<<"Value of str is:\n";
	cout<<str;
	getch();
}

Here is the sample output of this C++ program:

c++ input output stream

C++ standard input stream

Here is an example uses standard input stream in C++:

/* C++ Input and Output Stream */

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	char name[20];
	cout<<"Enter your name: ";
	cin>>name;
	cout<<"Your name is "<<name;
	getch();
}

Here is the sample run of the above C++ program:

c++ input output stream tutorial

C++ standard error stream

Here is an example helps you in understanding the standard error stream in C++:

/* C++ Input and Output Stream */

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	char str[]="Unable to read....";
	cerr<<"Error message: "<<str;
	getch();
}

Here is the sample run of this C++ program:

stream c++ example

Now, let's discuss about the standard log stream i.e. clog :

C++ standard log stream

Here is an example uses standard log stream in C++:

/* C++ Input and Output Stream */

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	char str[]="Unable to read...";
	clog<<"Error message: "<<str;
	getch();
}

C++ Input Output Stream Example

Here are some C++ input output stream example programs, demonstrating the uses of input and output streams in C++

/* C++ Input and Output Stream */

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	
	int num;
	char ch;
	char str[80];
	float f;
	
	cout<<"Enter a number: ";
	cin>>num;
	cout<<"\nYou entered "<<num;
	cout<<"\n\nEnter a character: ";
	cin>>ch;
	cout<<"\nYou entered "<<ch;
	cout<<"\n\nEnter a floating-point value: ";
	cin>>f;
	cout<<"\nYou entered "<<f;
	cout<<"\n\nEnter your name: ";
	cin>>str;
	cout<<"\nYou entered "<<str;
	cout<<"\n\nPress any key to exit...\n";
	
	getch();
}

Below is the sample run of the above C++ program:

input output stream

Let's take another C++ program:

/* C++ Input and Output Stream */

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	
	char sub1[20], sub2[20];
	float mark1, mark2;
	float sum=0, avg=0;
	
	cout<<"Enter two subject's name: ";
	cin>>sub1>>sub2;
	cout<<"\nEnter marks obtained in these two subjects: ";
	cin>>mark1>>mark2;
	sum=mark1+mark2;
	avg=sum/2;
	cout<<"\nYou got total "<<sum<<" marks with an average of "<<avg;
	cout<<"\nin two subjects namely "<<sub1<<" and "<<sub2;
	
	getch();
}

Here is the sample run of the above C++ program:

c++ input output stream example

More Examples

Here are some more C++ programs listed, that you can go for:


« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test