f



Re: Center formatted text with streams...

"GRalphE" <vaghn@yahoo.com> wrote in message

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

No.


> 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! ]
0
Siemel
6/26/2003 2:09:07 PM
comp.lang.c++.moderated 10738 articles. 1 followers. allnor (8509) is leader. Post Follow

3 Replies
517 Views

Similar Articles

[PageSpeed] 10

Hi,

Siemel Naran wrote:

> 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! ]
0
Maciej
6/26/2003 7:39:43 PM
On 26 Jun 2003 15:39:43 -0400, Maciej Sobczak <maciej@maciejsobczak.com>
wrote:

> 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! ]
0
John
6/27/2003 4:45:18 PM
"Siemel Naran" <SiemelNaran@KILL.att.net> writes:

> "Maciej Sobczak" <maciej@maciejsobczak.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 <class T>
>     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! ]
0
llewelly
6/28/2003 11:32:35 PM
Reply: