C++
  Home arrow C++ arrow Page 2 - General Stream Manipulation in C++
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

General Stream Manipulation in C++
By: Gabor Bernat
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2009-04-28

    Table of Contents:
  • General Stream Manipulation in C++
  • Numeric Systems and Bool Values
  • Float Formatting
  • Closing Thoughts

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More C++ Articles
    More By Gabor Bernat


     

    C++ ARTICLES

    - 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++
    - Advanced File Handling with Streams in C++
    - File Handling and Streams in C++
    - The STL String Class







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek