print on the same line

if I do


1
2
3
4
for (int i=0; i<4; i++)
{
     cout<<i<<endl;
}


I get:

0
1
2
3

How do I get it to print the on the same line like

0 1 2 3 ?

Thanks
Hi korivios,

use cout << i << " "; instead of cout << i << endl;. "endl" is the constant for a linebreak, thus ... << endl; results in a new line.

Greetings,
TheBear
No it's not. std::endl draws a newline and flushes the output buffer. I would say it's best to use '\n' instead of std::endl if you're drawing newlines frequently. It might get a little slow.
Last edited on
I thought, std::endl would be a OS dependent constant for a linebreak, so one doesn't need to fiddle around with "\r\n", "\r" and "\n". So i've learned another thing, thanks :-)
IIRC, the standard library for the most part (if not all of it) just takes a '\n' and then converts it to OS dependent version before output.
Obviously you should use std::endl every now-and-again. Personally I do it every other std::cout; unless they're inside a loop.
Topic archived. No new replies allowed.