One way to move on next line is cout << endl;
I know there is another way to move on next line. But, I do not know what is that. Can anyone tell me.
Asked
Active
Viewed 95 times
1
-
or `cout << '\n';` – RoiHatam May 13 '17 at 13:43
-
2@RoiHatam Why diddn't you write that as an answer? – πάντα ῥεῖ May 13 '17 at 13:45
-
@πάνταῥεῖ You are right – RoiHatam May 13 '17 at 13:47
-
This is a strange question. Why do you need two ways of doing the same thing? What is your programming problem that makes you think you need two ways to move to the next line? – Raymond Chen May 13 '17 at 14:20
2 Answers
2
You can write
std::cout << '\n';
instead. That would have the advantage that the stream isn't flushed at the same time.
Note:
On Windows OS it might be required to write
std::cout << '\r' << '\n';
or
std::cout << "\r\n";
to get correct line endings.

πάντα ῥεῖ
- 1
- 13
- 116
- 190
-
-
@Student28 If you use `using std::cout;` somewhere before you can do that, yes. – πάντα ῥεῖ May 13 '17 at 13:50
-
2I don't think the `\r\n` stuff is needed, iirc, the implementation maps the escape sequence `\n` to some proper newline sequence for the given platform. – Baum mit Augen May 13 '17 at 13:51
-
@Baum Might depend on the compiler used. But usually it's mapped, yes. – πάντα ῥεῖ May 13 '17 at 13:53
-
@πάνταῥεῖ -- you don't have to write `"\r\n"` on any compiler that conforms to the language definition. Inserting `'\n'` starts a new line; it's up to the runtime library to get this right. – Pete Becker May 13 '17 at 15:36
2
You can also use:
std::cout << '\n';
The only difference is that the stream won't be flushed.

RoiHatam
- 876
- 10
- 19
-
Also possible to use `std::cout << "\n"`. Net effect is the same, but the mechanism (e.g. which overload of `operator<<()`) differs – Peter May 13 '17 at 13:48
-
-
1You can only write `cout << '\n';` if you are using `using namespace std;` in the beginning of your code. – RoiHatam May 13 '17 at 13:54
-
1@Roi _"You can only write ..."_ Wrong! _`using namespace std;`_ [Infamous!](http://stackoverflow.com/q/1452721/14065) – πάντα ῥεῖ May 13 '17 at 13:55
-
-
-
@πάνταῥεῖ I didn't recommend it. He asked if he can use it so I answered. I will edit my comment. I meant the code won't be compiled without it. – RoiHatam May 13 '17 at 13:58
-
@Roi Just look what I've been commenting about that very same question. – πάντα ῥεῖ May 13 '17 at 14:01
-
Thank You. Actually I am a student and still use "using namespace std;" – Student28 May 13 '17 at 14:04
-
@Student28 _"Actually I am a student ..."_ How does that justify bad programming habits? – πάντα ῥεῖ May 13 '17 at 15:38
-
@Student28 But you are using it. What do you think `using namespace std;` does actually? Don't use code you don't understand yet. – πάντα ῥεῖ May 13 '17 at 17:45
-