stringstream + iomanip

This is a discussion on stringstream + iomanip within the C++ Programming forums, part of the General Programming Boards category; Code: stringstream Output; ... Output << setfill('0') << setw(pooldigits); Despite the above line, int values are not being padded. I've ...

  1. #1
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256

    stringstream + iomanip

    Code:
    stringstream Output;
    
    ...
    
    Output << setfill('0') << setw(pooldigits);
    Despite the above line, int values are not being padded. I've checked that the value of pooldigits is 3, just before the above line, and even tried specifying everything manually, as such.

    Code:
    Output.fill('0');
    Output.width(3);
    
    Output << 0 << endl;
    Even with the above code, Output is coming up with "0", instead of "000".

    Is there some inclusion or linking item I'm missing, or have I intermittently done something elsewhere in my code to break it? Has anyone encountered something similar before? I'll post more code if you guys need it, but this file alone is over a hundred lines.

    Thanks in advance!

    ~ Jake
    Code:
    void function(void)
     {
      function();
     }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,200
    What you have should work. Are you sure you're correctly compiling and executing the code? A common occurrence when things are not as they seem is that the program is edited, not recompiled, and therefore an older version of the executable is run.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    I've narrowed it down a bit more, by commenting out everything else in the same scope as this particular stringstream item. I've discovered the following...

    Code:
      Output.fill('0');
      Output.width(pooldigits);
    
      Output << 0 << endl;
    Correctly gives "000", but...

    Code:
      Output.fill('0');
      Output.width(pooldigits);
    
      Output << "ip dhcp pool " << 0 << endl;
    ...doesn't. Do these modifiers have a one-time-use limit?

    And yes, I've made sure I'm using the right files.
    Code:
    void function(void)
     {
      function();
     }

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,200
    Quote Originally Posted by Jaken Veina View Post
    Do these modifiers have a one-time-use limit?
    Generally, yes. When the value being formatted using those modifiers is output, the stream reverts to default state.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Actually, no. setw() is the only one that is auto-reset, all others keep their state.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    I had that feeling several times and I kept looking at the documentation for a hint that this was the case, but there doesn't seem to be any there. Adding setw() before every item that needs to be padded certainly does it. Thanks for the help.

    ~ Jake
    Code:
    void function(void)
     {
      function();
     }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stringstream Help...
    By neogst in forum C++ Programming
    Replies: 4
    Last Post: 02-08-2011, 09:23 AM
  2. Stringstream
    By lruc in forum C Programming
    Replies: 9
    Last Post: 03-23-2009, 02:27 PM
  3. help with iomanip
    By markucd in forum C++ Programming
    Replies: 1
    Last Post: 04-28-2006, 03:44 AM
  4. iomanip
    By ZerOrDie in forum C++ Programming
    Replies: 7
    Last Post: 09-28-2002, 06:26 AM
  5. Question about <iomanip.h>
    By bigblack in forum C++ Programming
    Replies: 1
    Last Post: 02-27-2002, 05:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21