setfill('\t')

A

Angel Tsankov

Is there any way to I can make sensible use of the tab character to
fill spaces in a right-justified display so that code like this

using namespace std;

cout
<< setiosflags( ios::fixed )
<< setw( 6 )
<< setfill( '\t' )
<< setprecision( 2 )
<< 1.23
<< endl
<< setiosflags( ios::fixed )
<< setw( 6 )
<< setfill( '\t' )
<< setprecision( 2 )
<< 12.23
<< endl;

produces
[\t]1.23
[\t]12.23

instead of
[\t][\t]1.23
[\t]12.23
 
U

usr.root

Angel Tsankov 写é“:
Is there any way to I can make sensible use of the tab character to
fill spaces in a right-justified display so that code like this

using namespace std;

cout
<< setiosflags( ios::fixed )
<< setw( 6 )
<< setfill( '\t' )
<< setprecision( 2 )
<< 1.23
<< endl
<< setiosflags( ios::fixed )
<< setw( 6 )
<< setfill( '\t' )
<< setprecision( 2 )
<< 12.23
<< endl;

produces
[\t]1.23
[\t]12.23

instead of
[\t][\t]1.23
[\t]12.23

no way ,pls,~_~
 
M

Mike Wahler

Angel Tsankov said:
Is there any way to I can make sensible use of the tab character to
fill spaces in a right-justified display so that code like this

using namespace std;

cout
<< setiosflags( ios::fixed )
<< setw( 6 )
<< setfill( '\t' )
<< setprecision( 2 )
<< 1.23
<< endl
<< setiosflags( ios::fixed )
<< setw( 6 )
<< setfill( '\t' )
<< setprecision( 2 )
<< 12.23
<< endl;

produces
[\t]1.23
[\t]12.23

instead of
[\t][\t]1.23
[\t]12.23

'setfill()' will cause subsequent output controlled by
'setw()' to 'pad' *every* unused character position encompassed
by the field width. So e.g. with a width of 6 and output of
1.23, you'll get two \t characters (or whatever the current
'setfill()' character is).

Also note that the specific behavior of \t will vary among
devices. (Might move current output position by four characters,
or eight, or none, etc. I.e. appearance of output containing
\t characters is not 'portable')

If you want to start your output at a given 'column' (whether
the result of a tab or a specific number of spaces), and left
justify it:

std::cout << std::left << std::setprecision(2);
std::cout << '\t' << std::setw(6) << 1.23 << '\n';
std::cout << '\t' << std::setw(6) << 12.23 << '\n';

/* (std::left is declared by <ios> )*/

(Note that the effects of stream manipulators remain until
specifically changed, with one exception: setw(), which is
automatically reset to setw(0) after each insertion. IOW
you don't need to repeat e.g. setprecision if the current setting
is appropriate.

-Mike
 
M

Mike Wahler

std::cout << std::left << std::setprecision(2);
std::cout << '\t' << std::setw(6) << 1.23 << '\n';
std::cout << '\t' << std::setw(6) << 12.23 << '\n';

With this format, the 'setw()' calls are no longer needed.

std::cout << '\t' << 1.23 << '\n';
std::cout << '\t' << 12.23 << '\n';

-Mike
 
A

Angel Tsankov

I agree that output containing \t characters is not portable.
However, I'm currently using tabs to indent source code, since this way
it is managed somewhat easier with VIsual Studio. I need to produce
pieces of such code automatically, that's why I started the topic. Of
course, I've considered the use of spaces instead of tabs for
indentation in order to avoid the portability problem (even across
applications), but since the tools I use have options to specify the
tab width, I decided to use tabs.
Is this the case with tabs on other OS's? Do users have any way to
specify tab width?
 
M

Mike Wahler

Angel Tsankov said:
I agree that output containing \t characters is not portable.
However, I'm currently using tabs to indent source code, since this way
it is managed somewhat easier with VIsual Studio. I need to produce
pieces of such code automatically, that's why I started the topic. Of
course, I've considered the use of spaces instead of tabs for
indentation in order to avoid the portability problem (even across
applications), but since the tools I use have options to specify the
tab width, I decided to use tabs.

If you only intend to display that output on systems
with the same tab behavior (whether default or configured),
that's not a problem.
Is this the case with tabs on other OS's?

Some might allow one to configure tab behavior. Others might not.
Also some applications have configurable tab behavior.
Do users have any way to
specify tab width?

Visual Studio does.

Note: Whatever systems or editor applications you use, when posting code
to Usenet, please convert tabs to spaces in your code (specifically
because of what I mentioned: not everyone's display will render tabs
the same way, and this is especially a problem for those with displays
which simply ignore tabs; it destroys all indenting, making the code
much less readable).

If the editor you're using to write code doesn't have the capability
of converting tabs to spaces, there are many free utilities on the
net which can do that. Google can find them.

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top