ostream operator and setw

  • Thread starter Christian Johannes Charbula
  • Start date
C

Christian Johannes Charbula

hi,
does anybody know how i can get the information what width for a
ostream is actually set?
i want to do somthing like the following, and need a way to do something
like stream.getw().

inline std::eek:stream& operator << (std::eek:stream &stream, const FixPoint
&fixPoint)
{
return
stream
// << std::setw (stream.getw()-fixPoint.m_fractionDigits-1)
<< std::setfill('0')
<< fixPoint.m_value / fixPoint.m_multiplicator << "."
<< std::setw (fixPoint.m_fractionDigits) << std::setfill('0')
<< fixPoint.m_value % fixPoint.m_multiplicator;
}

FixPoint test (42.30);
cout << setw (10) << test;

thanks,
chrisitian
 
A

Alf P. Steinbach

* Christian Johannes Charbula:
hi,
does anybody know how i can get the information what width for a
ostream is actually set?
i want to do somthing like the following, and need a way to do something
like stream.getw().

inline std::eek:stream& operator << (std::eek:stream &stream, const FixPoint
&fixPoint)
{
return
stream
// << std::setw (stream.getw()-fixPoint.m_fractionDigits-1)
<< std::setfill('0')
<< fixPoint.m_value / fixPoint.m_multiplicator << "."
<< std::setw (fixPoint.m_fractionDigits) << std::setfill('0')
<< fixPoint.m_value % fixPoint.m_multiplicator;
}

FixPoint test (42.30);
cout << setw (10) << test;

I read the documentation.

There found ios_base::width and ios_base::precision.

Next time, just read the documentation! ;-)


Cheers, & hth.,

- Alf
 
S

Stefan Naewe

hi,
does anybody know how i can get the information what width for a
ostream is actually set?
i want to do somthing like the following, and need a way to do something
like stream.getw().

inline std::eek:stream& operator << (std::eek:stream &stream, const FixPoint
&fixPoint)
{
return
stream
// << std::setw (stream.getw()-fixPoint.m_fractionDigits-1)

<< std::setw (stream.width()-fixPoint.m_fractionDigits-1)
<< std::setfill('0')
<< fixPoint.m_value / fixPoint.m_multiplicator << "."
<< std::setw (fixPoint.m_fractionDigits) << std::setfill('0')
<< fixPoint.m_value % fixPoint.m_multiplicator;
}

FixPoint test (42.30);
cout << setw (10) << test;

thanks,
chrisitian

S.
 
C

Christian Johannes Charbula

I read the documentation. fine.

There found ios_base::width and ios_base::precision.
aha, well that could help
Next time, just read the documentation! ;-)
i wouln't have ask here if i had found it there.
and how should i know that i've to look at ios_base?
i was just looking in ostream.

thanks for help, but remember i wouln'd have ask here if i were able to
locate it in the doco, the line in the middle would be enough help - the
other lines are needles.
christian
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

aha, well that could help

i wouln't have ask here if i had found it there.
and how should i know that i've to look at ios_base?
i was just looking in ostream.

thanks for help, but remember i wouln'd have ask here if i were able to
locate it in the doco, the line in the middle would be enough help - the
other lines are needles.
christian

There are lots of places on the net documenting the standard c++
library, www.cplusplus.com is one.
 
A

Alf P. Steinbach

* Christian Johannes Charbula:
aha, well that could help

i wouln't have ask here if i had found it there.
and how should i know that i've to look at ios_base?
i was just looking in ostream.

Depending on the documentation you have, to find all functionality for a
class you may have to look at base classes.

thanks for help, but remember i wouln'd have ask here if i were able to
locate it in the doco, the line in the middle would be enough help - the
other lines are needles.

Sorry that you feel that way. Hopefully, in the not too distant future,
you will learn that being taught how to fish and how to prepare and cook
your catch is much better than to be a given a ready broiled fish
whenever you ask. This time you got both the ready broiled fish and
some basics of fishing, but you /complain/ and whine that the latter is
equivalent to needling you for your lack of fishing expertise. What do
you do when nobody's in the mood to give you a ready broiled fish, and
nobody's any longer willing to try to teach you to fish, because all
they get for thanks is complaints and whining about needling you ("if I
wanted to know how to fish I wouldn't be asking for a fish!"), and you
don't know how to fish yourself and you're really hungry?

- Alf
 
A

Alf P. Steinbach

* Juha Nieminen:
"The" documentation? Which one would that be?

Your favorite at the moment. I used the Holy Standard. If I'd had
Visual Studio up and running I'd just used the help system.

Cheers, & hth.,

- Alf
 
C

Christian Johannes Charbula

Sorry that you feel that way. Hopefully, in the not too distant future,
you will learn that being taught how to fish and how to prepare and cook
your catch is much better than to be a given a ready broiled fish
whenever you ask. This time you got both the ready broiled fish and
some basics of fishing, but you /complain/ and whine that the latter is
equivalent to needling you for your lack of fishing expertise. What do
you do when nobody's in the mood to give you a ready broiled fish, and
nobody's any longer willing to try to teach you to fish, because all
they get for thanks is complaints and whining about needling you ("if I
wanted to know how to fish I wouldn't be asking for a fish!"), and you
don't know how to fish yourself and you're really hungry?

- Alf

;-)
explaining 'the fish u look for is anywhere in the water' is not
teaching fishing
 
C

Christian Johannes Charbula

Your favorite at the moment. I used the Holy Standard. If I'd had
Visual Studio up and running I'd just used the help system.

even the 'holy standard' (which i don't own) is helpless if u're looking
on the wronge place, as i did (ostream)
<°)))><
 
J

James Kanze

even the 'holy standard' (which i don't own) is helpless if
u're looking on the wronge place, as i did (ostream) <°)))><

I disagree. It most definitely says that basic_ostream derives
from basic_ios, which derives from ios_base, so you know that
you need to look in basic_ios and ios_base as well.

But of course, the standard is NOT a particularly good tutorial.
If you're asking such relatively basic questions, I'd suggest a
good tutorial first; something that explains in general how to
write a user defined operator<< or operartor>>, and explains the
conventions involving such an operator. (For example, if you
use width(), you're supposed to reset it to 0.) A good tutorial
will also discuss the trade offs between using existing << andwhere numeric types are involved, using existing << and >>, as
you do, is definitly the way to go. But this isn't always the
case.)

One such tutorial would be the chapter on iostream in "The
Standard C++ Library", by Nicolai Josuttis, but I'm sure that
there are others.
 

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

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top