Thread: Output formatting

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    21

    Output formatting

    Is there any way that I can format my output so that instead of outputting "1" it will output "0001"?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    #include <iomanip>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int value = 1;
    
        // Should be "1"
        cout << value << endl;
    
        // Should be "0001"
        cout << setfill('0') << setw(4) << value << endl;
    
        // Alternate way to do above... don't need to use the iomanip header
        cout.fill('0');
        cout.width(4);
        cout << value << endl;
    
        return 0;
    }
    Last edited by hk_mp5kpdw; 05-19-2005 at 12:37 PM.
    "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

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    21
    this is the line of code that I have put it into:

    Code:
    cout << setfill('0') << setw(4) << "\nID# for customer " << file[n].custname << " is " << file[n].custID << endl << endl;
    still doesn't seem to be working out...

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Rearrange it a little.
    Code:
    cout << "\nID# for customer " << file[n].custname << " is " << setfill('0') << setw(4) << file[n].custID << endl << endl;
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Try:

    Code:
    cout << "\nID# for customer " << file[n].custname << " is " 
         << setfill('0') << setw(4) << file[n].custID << endl << endl;
    The effect of the setw and setfill command disappear after the custname gets output so everything returns to "normal" (formatting-wise) by the time you output the custID. You should put the calls to them just before you output the value you need to have formatted like I have done above.
    Last edited by hk_mp5kpdw; 05-19-2005 at 01:31 PM.
    "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

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    21
    I changed the line around to look like this now:
    Code:
    cout << "\nID# for customer " <<  setfill('0') << setw(4) << file[n].custname << " is " << file[n].custID << endl << endl;
    now it works like a charm, thnx for the help

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    21
    whoops, wrong order there, that was an old iteration of it, I did get it to work though by just altering the order slightly, thnx again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch statement and returning the correct output
    By slowcoder in forum C Programming
    Replies: 6
    Last Post: 09-28-2006, 04:00 PM
  2. Formatting output to screen from a file
    By clearrtc in forum C Programming
    Replies: 2
    Last Post: 08-20-2006, 03:19 PM
  3. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  4. dos game help
    By kwm32 in forum Game Programming
    Replies: 7
    Last Post: 03-28-2004, 06:28 PM
  5. formatting output
    By spliff in forum C Programming
    Replies: 2
    Last Post: 08-14-2001, 06:50 PM