473,324 Members | 2,567 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

ostream value output question

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";
Jul 19 '05 #1
2 1439
"Parity" <pa****@comm.com> 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
Jul 19 '05 #2
> 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
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: joesoap | last post by:
Hi can anybody please tell me what is wrong with my ostream operator??? this is the output i get using the 3 attached files. this is the output after i run assignment2 -joesoap #include...
8
by: Boris | last post by:
Is it possible to manipulate the std::ostream to prepend a string when performing output, e.g. // manipute std::cout to prepend "prefix " std::cout << "hallo" << std::endl; // results in...
9
by: Ingo Nolden | last post by:
Hi there, I am writing c++ for some months now. I think I know the language now, but not yet all the tricky things of stl. For a kernel which is not using mfc I am writing a serialization. For...
4
by: Rock | last post by:
I'm in the process of writing this program for complex numbers and I use DevC++. My professor on the other hand compiles on Borland 5.5. So I ocasionally save and run my work on Borland to see if...
13
by: Peteroid | last post by:
These don't work (I'm using VS C++.NET 2005 Express with clr:/pure syntax): ostream& operator <<( ostream& output, String^ str ) { output << str ; //compile error return output ; } ...
6
by: silversurfer2025 | last post by:
Hello, I am currently trying to derive a class from ostream (which is giving output to my GUI), such that I can give my methods either std::cout or my own outputstream-class to be used as output...
1
by: Christopher Pisz | last post by:
I set out to make a custom logger. Examining some other code laying around, I came across one that derived from ostream, and had a associated class derived from streambuf. Is this practice a good...
9
by: tron.thomas | last post by:
This C program: #include <stdio.h> #include <math.h> int main() { float value; for(value = -1.0f; 1.1f value; value += 0.1f){ printf("%.1f\n", value);
3
by: Thomas Lenz | last post by:
The code below should allow to use a comma instead of << with ostreams and include a space between two operands when comma is used. e.g. cout << "hello", "world", endl; should print the line...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.