C++ standard library provides large possibilities for input and output. C++ uses an abstraction called stream
for input and output operations. A stream is an entity which is used by program to read or write characters. For example, screen, keyboard or a file are represented as streams in C++ Programming language. You do not need to know about details of the used entity or technical specification to work with it.
The following header files are commonly used for input and output in C++:
- iostream – is used to work with standard input stream, the standard output stream, the un-buffered standard error stream and the buffered standard error stream
- iomanip – is used for formatted input and output
- fstream – is used to work with files
Table of Contents
iostream
We used the iostream
header in the previous programs. You have to include iostream
header to work with different streams:
#include <iostream>
Now you can use the following streams for input and output.
Standard Output Stream – cout
The Standard Output Stream is used to output data on your screen. The object cout
of ostream
class is used for this purpose:
cout << "Hello " << endl;
cout object is used together with the insertion operator “<<” . endl is a function that inserts new line character and flushes the stream.
You can use cout object to output any built in data type. There is a possibility to use multiple << operators to output different elements by using only one cout object:
int age = 21; string name = "Kate"; cout << "My name is " << name << ". I\'m " << age << "years old." << endl;
Note: you have to include string
header to be able to output a variable of string type:
#include <string>
Standard Input Stream – cin
The Standard Input Stream usually represents the keyboards device. The cin
object of istream
class is used to get input from the keyboard:
string name; cout << "Enter your name" << endl; cin >> name;
The input is done by using extraction operator “>>”. This operator extracts data from stream and puts it into a variable:
cin >> name;
This line of code means that value of name variable now is set to a string entered from keyboard.
The extraction operator will “extract” characters from the keyboard until you didn’t press “Enter” key. You can make multiple inputs by using operator>> multiple times as you did it for multiple outputs with operator <<:
int age; string name; cout << "Enter your name and your age" << endl; cin >> name >> age;
Un-buffered standard error stream – cerr
Un-buffered standard error stream is used when you need to show an error message immediately. This stream is attached to the default error device. For your computer the standard error device is your screen too. You can use un-buffered standard error stream accessing cerr object which is an instance of ostream
class. The use of cerr
is similar to the use of cout
. The difference consists in the fact you use your error stream for output and not standard output stream:
//display error message cerr << "Something went wrong..." << endl;
buffered standard error stream – clog
clog object of ostream class represents buffered standard error stream. The difference between buffered and un-buffered error stream consists in the fact that each insertion to clog object causes the output to be held in a buffer until the buffer is filled or flushed. The message will be displayed on your screen too. The use of clog object is the same as use of any other objects of ostream class:
clog << "Unable to open file" << endl;
While you are studying C++ language you will notice a quite difference in use of different streams. But it is a good habit to use error streams in the case when you need to show information about any error.
iomanip
The iostream
library provides possibilities for input and output but you can’t specify the format of the data to output/input. For this purpose you can use iomanip library:
#include <iomanip>
iomanip provides the following possibilities for input and output format:
setbase
setbase (int base) – function that sets base for the number’s output. The base can be octal, decimal and hexadecimal:
int a = 160; cout << "Octal base " << setbase(8) << a << endl; cout << "Decimal base " << setbase(10) << a << endl; cout << "Hexadecimal base " << setbase(16) << a << endl;
The output is:
Octal base 240
Decimal base 160
Hexadecimal base a0
setw
setw (int n) – function used to set the width of the output. The parameter is field’s width:
cout << "123456789" << endl; cout << setw(9) << "x" << endl;
The output in this case is:
123456789
x
As you can see, even the “x” length is only one, its displayed width is 9 characters.
setfill
setfill (char_type c) – function used to set a fill character. In the previous example :
cout << setw(9) << "x" << endl;
The width is set to 9, but the length of output string is 1. The other 8 characters that precede “x” are blank characters. You can set new fill character for the stream using setfill
function:
cout << "123456789" << endl; cout <<setfill('0') << setw(9) << "x" << endl;
‘0’ is set as fill character:
123456789
00000000x
setprecision
setprecision (int n); – sets decimal precision for output of floating points values. Parameter n is the value of decimal precision:
double oneThird = 1.0 / 3.0; cout << setprecision(1) << oneThird << endl; cout << setprecision(3) << oneThird << endl; cout << setprecision(5) << oneThird << endl;
The variable to be shown in output has different length of the decimal part according to the precision that is set:
0.3
0.333
0.33333
setiosflags
setiosflags (ios_base::fmtflags mask); – the output format of the stream is specified by the format flags. You can set a flag to specify the format of the output by using setiosflags functions. The parameter for this function is of type fmtflags. There is a possibility to set following format flags using format masks:
field | Format mask | Effect |
independent flags | Boolalpha | Read or write bool elements as true and false. |
Showbase | Write values preceded by their numeric base prefix. | |
showpoint | Write floating-point values including always the decimal point. | |
showpos | Write non-negative numerical values preceded by a “+”. | |
skipws | Skip leading whitespaces | |
unitbuf | Flush output after each inserting operation. | |
uppercase | replace lowercase letters with uppercase letters | |
numerical base (basefield) | dec | Read or write values using decimal base format. |
hex | Read or write values using hexadecimal base format. | |
oct | Read or write values using octal base format. | |
float format (floatfield) | fixed | Write floating point values in fixed-point format. |
scientific | Write floating-point values in scientific format. | |
adjustment (adjustfield) | internal | The output is padded to a specific internal point |
left | the output is padded to the left | |
right | the output is padded to the right |
You can set multiple masks by using | operation:
cout << setw(10) << setiosflags(std::ios::left)<< "Hello " << "World!" << endl; //set numeric base to 16 , show plus sign and show base cout << hex << setiosflags(std::ios::showpos | std::ios::showbase) << 160 << endl; double var = 0.000000000001; //show a double value in decimal base and hexadecimal base cout << var << " = " << setiosflags(std::ios::floatfield) << var << endl;
The output is:
Hello World!
0xa0
+1e-012 = +0x1.197998p-40
resetiosflags
resetiosflags (ios_base::fmtflags mask) – resets the flag specified by the mask.
It has the opposite effect in comparison with setiosflags
.