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

ostream operator and setw

P: n/a
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::ostream& operator << (std::ostream &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
Sep 14 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On 9/14/2007 11:26 AM, Christian Johannes Charbula wrote:
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::ostream& operator << (std::ostream &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.
--
Stefan Naewe stefan dot naewe at atlas-elektronik dot com
Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please http://www.expita.com/nomime.html
Sep 14 '07 #2

P: n/a
Stefan Naewe schrieb:
<< std::setw (stream.width()-fixPoint.m_fractionDigits-1)
thank you this was realy helpful.
christian
Sep 14 '07 #3

P: n/a
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
Sep 14 '07 #4

P: n/a
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)
<°)))><
Sep 14 '07 #5

P: n/a
On Sep 14, 4:35 pm, Christian Johannes Charbula
<charb...@racon-linz.atwrote:
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) <°)))><
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 << and
>operators, and using the streambuf directly. (In your case,
where 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.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 15 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.