costume manipulators

i have the following manipulator in my fsu namespace
How can i make s.fill reset to default after each use?
1
2
3
4
5
6
7
8
9
std::ios& fsu::HexFill (std::ios& s)                                                                                                                                              
{                                                                                                                                                                                 
  s.setf(std::ios::hex,std::ios::basefield);                                                                                                                                      
  s.setf(std::ios::uppercase);                                                                                                                                                    
  s.fill('0');                                                                                                                                                                    
                                                                                                                                                                                  
  return s;                                                                                                                                                                       
                                                                                                                                                                       
}    

when I do the following
1
2
3
4
5
	for(int i =0 ; i < 4; i++)
	{
	std::cout<<std::setw(5)<<' '<<std::setw(16)<<fsu::HexFill<<number<<std::endl;	
	
	}

The blank lines are also getting filled with 0 . I am sure its because the stream is set to fill with (0). How can I make the manipulator HexFill display zeroes only for the number and not the blank lines ?
Last edited on
Something like the following might work better for you:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <iomanip>

struct hex
{
    hex(int val) : value(val){ }
    int value;
};

std::ostream& operator<<(std::ostream& os, hex h)
{
    auto oldflags = os.flags();
    auto oldfill = os.fill();

    os.setf(std::ios::hex, std::ios::basefield);
    os.setf(std::ios::uppercase);
    os.fill('0');

    os << h.value;

    os.fill(oldfill);
    os.setf(oldflags);
    return os;
}

int main()
{
    for (int i = 0; i < 4; i++)
        std::cout << std::setw(5) << ' ' << std::setw(16) << hex(i) << std::endl;
}


http://ideone.com/5vv6Df
that is a good idea however the professor stated that we are only allowed
to implement the manipulators using std::ios& .
Well, your options are limited for manipulators. You want it to work the way std::setw does, but std::setw calls ios::width, and the width is reset to 0 on insertion or extraction from a stream. There is no corresponding functionality for ios::fill.

Perhaps another manipulator (fsu::BlankFill, maybe) could be used to reset the fill characters, making the code look like:

1
2
for ( int i = 0; i<4;  ++i )
    std::cout << std::setw(5) << fsu::BlankFill << ' ' << std::setw(16) << fsu::HexFill << i << std::endl;


Or supply the width with the fill manipulator to make things a little more compact:
1
2
for ( int i = 0; i<4;  ++i )
    std::cout << fsu::BlankFill(5) << ' ' << fsu::HexFill(16) << i << std::endl;


Of course, you could always just save and restore state on each iteration of the for loop.

Related reading:
http://stackoverflow.com/a/1533222/1708463
http://stackoverflow.com/questions/799599/c-custom-stream-manipulator-that-changes-next-item-on-stream

[Edit: Btw, was that supposed to be "custom" in the subject? ]
Last edited on
okay thanks a lot I will do it with fsu::BlankFill . hahah yeah custom oops its not like the manipulators are dressing as batman for a Halloween party .
Topic archived. No new replies allowed.