C++
  Home arrow C++ arrow Page 8 - More on Handling Basic Data Types
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 
Sun Developer Network 
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++

More on Handling Basic Data Types
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2005-03-02

    Table of Contents:
  • More on Handling Basic Data Types
  • Try It Out: Explicit Casting
  • Finding Out About Types
  • Try It Out: Finding the Sizes of Data Types
  • Using the Bitwise AND
  • Using the Bitwise Exclusive OR
  • Try It Out: Using the Bitwise Operators
  • More on Output Manipulators
  • Enumerated Data Types
  • Try It Out: Enumerated Data Types
  • The Lifetime of a Variable
  • Try It Out: The Scope Resolution Operator
  • Declaring External Variables

  • 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


    More on Handling Basic Data Types - More on Output Manipulators


    (Page 8 of 13 )

    Taking the last chapter into account, you’ve now seen five of the modal output manipulators that the <iostream> header defines: scientific, fixed, dec, hex, and oct. The time seems right, therefore, to list these and all the other similar manipulators in one place (see Table 3-2). Don’t worry for now about the bool values mentioned in the last two entries—they’re coming up in the next chapter.

    Table 3-2. Output Manipulators

    Manipulator Action Performed
    dec Formats integer values as base 10 (decimal). This is the default
    representation.
    hex Formats integer values as base 16 (hexadecimal).
    oct Formats integer values as base 8 (octal).
    left Left-aligns values in the output field and pads them on the right with the
    fill character. The default fill character is a space, as you’ve seen.
    right Right-aligns values in the output field and pads them on the left with the
    fill character. This is the default alignment.
    fixed Outputs floating-point values in fixed-point notation—that is, without an
    exponent.
    scientific Outputs floating-point values in scientific notation—that is, as the
    mantissa plus an exponent. The default mode selects fixed or scientific
    notation, depending on the value to be displayed.
    showpoint Shows the decimal point and trailing zeros for floating-point values.
    noshowpoint The opposite of the showpoint manipulator. This is the default.
    showbase Prefixes octal output with 0 and hexadecimal output with 0x or 0X.
    noshowbase Shows octal and hexadecimal output without the prefix. This is the
    default.
    showpos Shows plus signs (+) for positive values.
    noshowpos Doesn’t show plus signs for positive values. This is the default.
    uppercase Displays uppercase A through F for hexadecimal digits when outputting
    integers in hexadecimal format and 0X if showbase is set. Displays E for
    the exponent when outputting values in scientific notation, rather than
    using lowercase e.
    nouppercase Uses lowercase for the preceding items. This is the default.
    boolalpha Displays bool values as true and false.
    noboolalpha

    Displays bool values as 1 and 0.

     


    You may want to set more than one of these modes at a time, and one way to do this is to insert multiple manipulators into the stream. For example, if you want to output your integer data as hexadecimal values that are left aligned in the output field, you could write

     std::cout << std::hex << std::left << value;

    which will output value (and all subsequent integers in the program, unless the settings are changed) as a left-justified hexadecimal number.

    Table 3-3 shows the manipulators that expect you to supply an argument value.

    Table 3-3. Output Manipulators That Require an Argument Value

    Manipulator Action Performed
    setfill() Sets the fill character as specified by the argument. The default fill
    character is a space.
    setw() Sets the field width as specified by the argument.
    setprecision() Sets the precision for floating-point values as specified by the argument.

    The precision is the number of decimal digits in the output.


    This article is excerpted from Beginning ANSI C++ The Complete Language by Ivor Horton (Apress, 2004; ISBN  1590592271). Check it out at your favorite bookstore today. Buy this book now.

    More C++ Articles
    More By Apress Publishing


     

    C++ ARTICLES

    - Paths and Files
    - Directories in C++
    - Focusing on C++ Files
    - Const Correctness in C++
    - Manipulating Streams and Files with C++
    - Streams and Files
    - Multiplying Large Numbers with Karatsuba`s A...
    - Large Numbers
    - Dijkstra`s Shunting Algorithm with STL and C...
    - Brief Introduction to the STL Containers
    - The Standard Template Library
    - Templates in C++
    - C++ Programmer Alerts
    - C++ Programming Tips
    - First Steps in (C) Programming, conclusion






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT