Thread: Question regarding the fill function.

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    38

    Question regarding the fill function.

    I was wondering how to use the fill function to place leading 0's before my integer value, provided it's not of a certain size. In the program I'm writing, I'm supposed to accept a membership card number. This number can be between 1 and 999999 inclusive. The formatting is supposed to be 12-3456, but whenever the number is less than 6 digits long, the format is 00-0234, etc. I was trying to use the fill function to do so, with no luck. I've separated the number into two integers representing the first two digits and the last four, I just can't seem to get the leading zeroes. Any ideas?


    - Chris

  2. #2
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Something like this, perhaps?
    Code:
        int no = 000456;
        
        stringstream sout;
        sout << setfill('0') << setw(6) << no;
        string s = sout.str();
        s.insert(2,"-");
        cout << s;
        cin.get();
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    38
    Used something similar and it works great. Done my project 2 hours before the due date, and I didn't even procrasinate. What a mess =D. Thanks a bunch.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Question on function syntax and calling function
    By cbrman in forum C Programming
    Replies: 10
    Last Post: 10-05-2003, 05:32 PM