C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Re: Center formatted text with streams...

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Siemel Naran
Guest





PostPosted: Thu Jun 26, 2003 2:09 pm    Post subject: Re: Center formatted text with streams... Reply with quote



"GRalphE" <vaghn (AT) yahoo (DOT) com> wrote in message

Quote:
If I turn on both left and right, would that produce centered text?

No.


Quote:
Just a lame question.

No.


BTW, what should this do

cout << myspace::center << "hello" << 3.0;

Should the "hello" be centered then the 3.0 after it, or should "hello
3.0"
be centered?


Maybe you can write all the text you want centered into a string, then
write
a manipulator to center the string.

There is no portable way to get the number of characters in a line.
Let's
assume it is 80, though perhaps you can use a non-portable function to
get
it like gettextinfo in Windows.

cout << myspace::center(ostringstream() << "hello" << 3.0);

where

class center {
public:
center(ostream& text); // may throw bad_cast
private:
ostringstream& d_text;
};

ostream& operator<<(ostream& out, center c) {
const int screenwidth = 80;
const string& text = d_text.str();
const int textwidth = int(text.size());
int space = (screenwidth - textwidth) / 2;
if (space < 0) space = 0;
out.setfill(' ');
out << setw(space) << "" << text << 'n';
return out;
}


--
+++++++++++
Siemel Naran


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Siemel Naran
Guest





PostPosted: Thu Jun 26, 2003 2:24 pm    Post subject: Re: Center formatted text with streams... Reply with quote



"GRalphE" <vaghn (AT) yahoo (DOT) com> wrote in message

Quote:
If I turn on both left and right, would that produce centered text?

No.


Quote:
Just a lame question.

No.


BTW, what should this do

cout << myspace::center << "hello" << 3.0;

Should the "hello" be centered then the 3.0 after it, or should "hello
3.0"
be centered?


Maybe you can write all the text you want centered into a string, then
write
a manipulator to center the string.

There is no portable way to get the number of characters in a line.
Let's
assume it is 80, though perhaps you can use a non-portable function to
get
it.

cout << myspace::center(ostringstream() << "hello" << 3.0);

where

class center {
public:
center(ostream& text); // may throw bad_cast
private:
ostringstream& d_text;
};

ostream& operator<<(ostream& out, center c) {
const int screenwidth = 80;
const string& text = d_text.str();
const int textwidth = int(text.size());
int space = (screenwidth - textwidth) / 2;
if (space < 0) space = 0;
out.setfill(' ');
out << setw(space) << text << 'n';
return out;
}


--
+++++++++++
Siemel Naran



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Maciej Sobczak
Guest





PostPosted: Thu Jun 26, 2003 7:39 pm    Post subject: Re: Center formatted text with streams... Reply with quote



Hi,

Siemel Naran wrote:

Quote:
cout << myspace::center(ostringstream() << "hello" << 3.0);

Except that ostringstream() above will create a temporary and the first
operator<< (the one on the right of ostringstream) is non-member
function accepting non-const reference to its left parameter. It is not
possible to bound temporaries to non-const references.

In other words, it is not possible to use temporary stream to insert a
string into it. Funny: you can insert numeric values this way with no
problem.

Even more fun:

ostringstream() << "hello" << 3; // error for the reasons above
ostringstream() << 3 << "hello"; // OK

--
Maciej Sobczak
http://www.maciejsobczak.com/

Distributed programming lib for C, C++, Python & Tcl:
http://www.maciejsobczak.com/prog/yami/


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
John Potter
Guest





PostPosted: Fri Jun 27, 2003 4:44 pm    Post subject: Re: Center formatted text with streams... Reply with quote

On 26 Jun 2003 10:24:12 -0400, "Siemel Naran" <SiemelNaran (AT) KILL (DOT) att.net>
wrote:

Quote:
BTW, what should this do

cout << myspace::center << "hello" << 3.0;

Should the "hello" be centered then the 3.0 after it, or should "hello
3.0" be centered?

It should produce "hello3" because the width is 0 for both items.
Padding makes no sense without a setw.

cout << myspace::center << setw(9) << "hello" << setw(Cool << 3.0

would give " hello 3 ". Note that it is sticky like all
formatters. How do you clear it?

John

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
John Potter
Guest





PostPosted: Fri Jun 27, 2003 4:45 pm    Post subject: Re: Center formatted text with streams... Reply with quote

On 26 Jun 2003 15:39:43 -0400, Maciej Sobczak <maciej (AT) maciejsobczak (DOT) com>
wrote:

Quote:
ostringstream() << "hello" << 3; // error for the reasons above

You just need to know how to cheat.

ostringstream() << flush << "hello" << 3;

John

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
llewelly
Guest





PostPosted: Sat Jun 28, 2003 11:32 pm    Post subject: Re: Center formatted text with streams... Reply with quote

"Siemel Naran" <SiemelNaran (AT) KILL (DOT) att.net> writes:

Quote:
"Maciej Sobczak" <maciej (AT) maciejsobczak (DOT) com> wrote in message

cout << myspace::center(ostringstream() << "hello" << 3.0);

Except that ostringstream() above will create a temporary and the first
operator<< (the one on the right of ostringstream) is non-member
function accepting non-const reference to its left parameter. It is not
possible to bound temporaries to non-const references.

Good point.

You can have center's constructor center::center(const std::string&).

Or you can write a class my_ostringstream that holds an ostringstream or
derives from one, and write a member template operator<<.

class my_ostringstream : public std::ostringstream {
public:
template my_ostringstream& operator<<(const T& arg) const {
std::ostringstream << arg;
return *this;
}
};

The center's constructor can take a my_ostringstream& as an argument and
there's no need for dynamic_cast.


In other words, it is not possible to use temporary stream to insert a
string into it. Funny: you can insert numeric values this way with no
problem.

Even more fun:

ostringstream() << "hello" << 3; // error for the reasons above
ostringstream() << 3 << "hello"; // OK

Why is the 2nd one OK?

Because you can pass a temporary to a non-const member function.
However you cannot bind a temporary to a reference to non-const.

More unecessary incompatibility between member and non-member
functions. More reason to prefer non-member functions to member
functions, wherever reasonable.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Mon Jun 30, 2003 4:04 pm    Post subject: Re: Center formatted text with streams... Reply with quote

John Potter <jpotter (AT) falcon (DOT) lhup.edu> wrote

Quote:
On 26 Jun 2003 10:24:12 -0400, "Siemel Naran"
[email]SiemelNaran (AT) KILL (DOT) att.net[/email]
wrote:

BTW, what should this do

cout << myspace::center << "hello" << 3.0;

Should the "hello" be centered then the 3.0 after it, or should
"hello
3.0" be centered?

It should produce "hello3" because the width is 0 for both items.
Padding makes no sense without a setw.

cout << myspace::center << setw(9) << "hello" << setw(Cool << 3.0

would give " hello 3 ". Note that it is sticky like all
formatters. How do you clear it?

The same way you clear ios::left and ios::right ?

--
James Kanze GABI Software
mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/
http://www.gabi-soft.fr
Beratung in objektorientierter
Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, Tél. : +33 (0)1 30 23 45
16

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.