Input / Output Streams

I/O is provided via the standard library.

 

The ios class is derived from ios_base

 

The istream class (derived from ios) is used for input streams. The extraction operator >> is used to remove values from the stream

 

The ostream class (derived from ios) is used for output streams. The insertion operator << is used to place values into the stream

 

The iostream is used for both input and output streams, and is most commonly used.

 

I/O methods are contained within the std namespace, and hence 'using namespace std ;' is utilised to avoid having to write std:: before each method, e.g. std::cin

 

Standard streams:

  • cin - istream_withassign class associated with the standard input: the keyboard
  • cout - ostream_withassign class associated with the standard output: the monitor
  • cerr - ostream_withassign class associated with the standard output: the monitor, unbuffered
  • clog - ostream_withassign class associated with the standard output: the monitor, unbuffered

Basic example showing standard streams used for input and output:

#include <iostream>
using namespace std;

int main () {

	cout << "Please enter the product of 6 times 7:" << endl ;

	int number ;

	cin >> number;

	if (number != 42){
		cerr << "Hmm, perhaps you should go back to school!" << endl ;
		return 1 ;
	}

	cout << "Excellent! " << number << " is correct." << endl ;

	return 0;
}

Compile & Run:

Please enter the product of 6 times 7:
13
Hmm, perhaps you should go back to school!

Leave a Reply