Formatting
So far in the course we used simple unformatted output. We wrote a statement like:
cout << " This is my result \n " << sum;
which produced the string "This is my result " and the data stored in the location named sum. Of course the ability to change line using \ n is a formatting ability we had. Similarly if we wanted to leave space between two outputted numbers we can always send an empty string in between as in the example:
cout << a << " " << b;
However we would like to have better tools in producing good-looking output. Engineers prefer to communicate their numerical results and summaries in form of tables. You have heard the saying " a picture is worth a thousand words". Tables are almost as valuable as pictures ( graphs ) in presenting numerical data, but in addition they can be very precise. And we all know the fixation of engineers with precision ( a bridge cannot afford to nearly stand) . In order to produce this sort of orderly output we use the following functions, which are contained in the <iomanip.h> header:
For example if we have
In addition, if we have:
In Visual C++ , when we want to switch from fixed format to scientific or vice versa, we must send to the stream the command resetiosflags as shown in the example below: cout << setiosflags( ios :: fixed ) << temperature ; cout << resetiosflags( ios :: fixed ) ; cout << setiosflags( ios :: scientific) << pressure ; |
|||||||||||
|
|||||||||||
Jacob Y. Kazakia © 2001 All rights reserved |