In C I have always used \n to do a new line. Now from many programs in C++ in my studies I notice from C++ that the endl was added to do a new line...When would you use it when you can use "\n" ??? Thanks for the help
In C I have always used \n to do a new line. Now from many programs in C++ in my studies I notice from C++ that the endl was added to do a new line...When would you use it when you can use "\n" ??? Thanks for the help
>When would you use it when you can use "\n" ???
If you want to flush the stream immediately after printing a newline, use endl. Otherwise, use '\n'.
My best code is written with the delete key.
WHen would you be in need to flush the stream immediately?
>WHen would you be in need to flush the stream immediately?
When you're prompting a user for input. On some systems if you don't flush the stream, the prompt will not print and the user won't know what to do as the program hangs while waiting for input.
My best code is written with the delete key.
Alright, thanks. I'll use endl right before I cin input. Thanks again prelude
I've had some really screwy results using \n on a Windows system... I've had stuff display in the wrong sequence!!! That might be a bug in my compiler, but it sure was a confusing problem.
So, now I always use endl, and I recommend that you use endl unless you have a specific reason (maybe speed?) for not flushing the buffer immediately.
[EDIT] - As you probably know, there is no endl in C. It's 'new' to C++ and is part of <iostream> (specifically <ostream> ).
Just in case you're interested, endl does add \n before flushing the buffer. From the Dinkumware reference:The manipulator calls ostr.put(ostr. widen('\n')), then calls ostr.flush(). It returns ostr.
Last edited by DougDbug; 04-15-2004 at 12:46 PM.
Is there overhead associated with flushing the buffer? For example if I want to put a bunch of end lines, should i use:
orCode:cout << endl << endl << endl << endl;
Code:cout << \n\n\n << endl;
>Is there overhead associated with flushing the buffer?
Yes.
>For example if I want to put a bunch of end lines
You should generally use '\n' for the intermediate newlines and then endl to finish things up by printing the last newline and flushing the buffer.
My best code is written with the delete key.