Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > ostream value output question

Reply
Thread Tools

ostream value output question

 
 
Parity
Guest
Posts: n/a
 
      08-13-2003
Is there some standard) way to make ostream automatically insert
whitespace character after something is inserted into stream using
operator <<?
For example:

int x=10, y=20, z=30;
ofs << "(" << x << y << z << ")\n";

should yield "(10 20 30)" - not "(102030)". My current way around this
induces very messy code, especially when there are more than three
variables written at once:

ofs << "(" << x << " " << y << " " << z << ")\n";
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-13-2003
"Parity" <(E-Mail Removed)> wrote...
> Is there some standard) way to make ostream automatically insert
> whitespace character after something is inserted into stream using
> operator <<?


Something? Like what? Like, everything?

> For example:
>
> int x=10, y=20, z=30;
> ofs << "(" << x << y << z << ")\n";
>
> should yield "(10 20 30)" - not "(102030)".


If it inserts a space after "everything", it should yield

"( 10 20 30 )\n "

, shouldn't it? If it inserts a space only after an int,
it will result into

"(10 20 30 )\n"

, won't it? Both are not what you need, I gather.

> My current way around this
> induces very messy code, especially when there are more than three
> variables written at once:
>
> ofs << "(" << x << " " << y << " " << z << ")\n";


THAT you call messy? At least it does what you want, not what
it thinks you want. You could probably write a custom stream
buffer that would append a space to itself after every output.
But that will result in the output I quoted above, not in what
you think it should.

Victor


 
Reply With Quote
 
 
 
 
Stephen Howe
Guest
Posts: n/a
 
      08-13-2003
> Is there some standard) way to make ostream automatically insert
> whitespace character after something is inserted into stream using
> operator <<?
> For example:
>
> int x=10, y=20, z=30;
> ofs << "(" << x << y << z << ")\n";
>
> should yield "(10 20 30)" - not "(102030)". My current way around this
> induces very messy code, especially when there are more than three
> variables written at once:
>
> ofs << "(" << x << " " << y << " " << z << ")\n";


You can do

ofs << '(' << x << ' ' << y << ' ' << z << ")\n";

which is more economical than quoted strings. I use single characters where
the quoted string being output is just a single character.

Apart from this, you can use ostream_iterator if your x, y,z are in an
array. So

vector <int> coords1;
int coords2[3];
:
ofs << '(';
copy(coords1.begin(), coords1.end(), ostream_iterator<int>(ofs, " "));
ofs << ")\n";

or (I have not tested this out to see if this compiles but I think so)

ofs << '(';
copy(&coords2[0], &coords2[3], ostream_iterator<int>(ofs, " "));
ofs << ")\n";

The " " adds a space between elements of an array

with the appropriate headers and "using namespace std;"

Stephen Howe


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange compiler error w.r.t 'friend ostream& operator<< <>(ostream&,...)' abhay.burli@gmail.com C++ 2 02-26-2009 06:18 AM
Formating Output of std::ostream Johannes Barop C++ 10 12-27-2005 09:48 PM
friend ostream& operator <<, compiles and runs in Dev++ no output or error in Borland Rock C++ 4 11-20-2005 06:26 AM
Please help on ostream output error Zhixin Han C++ 4 10-07-2004 03:12 PM
function to format message & output to cerr/cout/ostream Trevor C++ 2 11-02-2003 11:51 PM



Advertisments