By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,500 Members | 1,769 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,500 IT Pros & Developers. It's quick & easy.

setfill('\t')

P: n/a
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

Nov 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/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


no way ,pls,~_~

Nov 22 '05 #2

P: n/a

"Angel Tsankov" <Bi****@abv.bg> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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
Nov 22 '05 #3

P: n/a

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:vv*****************@newsread3.news.pas.earthl ink.net...

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
Nov 22 '05 #4

P: n/a
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?

Nov 22 '05 #5

P: n/a

"Angel Tsankov" <Bi****@abv.bg> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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
Nov 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.