Thread: Formatting an integer

  1. #1
    Unregistered
    Guest

    Formatting an integer

    Hi all,

    Here's the line of code:

    -----------------------------------------
    int index = 6;

    cout << index << endl;

    -----------------------------------------

    I want it print 06 instead of 6. How do I do it?

    thanks.

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    cout<<"0"<<index<<endl;

  3. #3
    Unregistered
    Guest
    There are other ways than the posted technique, but they aren't as straightforward. If the above is good enough for you so be it. If you want to learn about the alternatives, look up ostream modifiers and manipulators or printf() formatting techniques.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int index = 6;
        cout.fill('0');
        cout.width(2);
        cout << index << endl;
        return 0;
    }
    Or...

    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
        int index = 6;
        cout << setfill('0') << setw(2) << index << endl;
        return 0;
    }
    Each time you output a value, things will go back to the defaults so you will need to reapply the fill and width commands before each value you are outputting.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    or u could prolly jus say

    int var = 06;
    cout<<var<<endl;
    hooch

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Nope, won't work if you do that.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    hmm i guess i should of tested it first
    hooch

  8. #8
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    You would think this is nuclear science...





    >>>cout<<"0"<<index<<endl;

    nice work govt
    Blue

  9. #9
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    it jus might be nuclear science heh
    hooch

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  5. dos game help
    By kwm32 in forum Game Programming
    Replies: 7
    Last Post: 03-28-2004, 06:28 PM