Home arrow C++ arrow Page 2 - General Stream Manipulation in C++
C++

General Stream Manipulation in C++


Using streams in C++ is an easy and intuitive task. However, sometimes you may want to customize the input/output on a deeper level. This is possible in a powerful and easy way with the use of the manipulators.

Author Info:
By: Gabor Bernat
Rating: 5 stars5 stars5 stars5 stars5 stars / 10
April 28, 2009
TABLE OF CONTENTS:
  1. · General Stream Manipulation in C++
  2. · Numeric Systems and Bool Values
  3. · Float Formatting
  4. · Closing Thoughts

print this article
SEARCH DEVARTICLES

General Stream Manipulation in C++ - Numeric Systems and Bool Values
(Page 2 of 4 )

In this section we will talk about how you can change the numeric system in which you display a number, but first I'll show you a little trick for telling the cout stream to write the bool values in a more common language. Surely every programmer will know that zero is false and anything else true, but why not make that obvious to anyone?

For this, there are the boolaplha/noboolaplha flags:


cout << boolalpha<< false << " and " << true << noboolalpha << endl;

cout << false << " and " << true << endl;


false and true

0 and 1

Press any key to continue . . .


First, we printed in the more sophisticated way with the flag on. The second line (actually the third in the code snippet) is how they will get the default settings. From this you could already point out the permanent state of the flags. As noted at the end of first line, we changed the flag back to the default behavior; the noboolalpha is set on by default.

In addition, here we should cover the endl manipulator that puts in a new line and flushes the stream. You may also put just a zero char in the console if you apply the ends manipulator. Note the empty space in the example below before the “Press” word.


char a= 'a';

cout << a << ends;


a Press any key to continue . . .


Now how about number systems? We all know that by default, we work in the decimal number system, but sometimes we may want to express/print the number in a different one. The octal and hexadecimal systems are used near the decimal one, so only for these the basic stream manipulators offer support. For others you may write your own one.


int a = 100;

 

cout << oct << a << endl ;

cout << dec << a << endl;

cout << hex << a << endl;

cout << setbase(9) << a << endl;


cout << showbase<< endl;


cout << oct << a << endl ;

cout << dec << a << endl;

cout << hex << a << endl;


144

100

64

100


0144

100

0x64

Press any key to continue . . .


In the example above, I have already explained everything, but let’s go through it once again for the sake of detail. For setting a specific number in you system, you apply the corresponding manipulator to the stream: the oct stays for the octal number system, the dec for the decimal and the hex for the hexadecimal.

You can also set base explicitly though the setbase() manipulator (be warned; as this requires an argument as input, you need to include the iomanip), but calling it on anything other than 8, 10, or 16 is considered invalid input, and the decimal default will be forced. Now all that remains is learning how we tell the system in which that the number on the screen is displayed.

In order to accomplish this, the so-called showbase manipulator was made. It will display nothing in front of number if it is a decimal, add a “0” if it is in the octal system and a “0X” if the hexadecimal number system is the rule. By default, this is off.


blog comments powered by Disqus
C++ ARTICLES

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials