question about endl

This is a discussion on question about endl within the C++ Programming forums, part of the General Programming Boards category; In C I have always used \n to do a new line. Now from many programs in C++ in my ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    53

    question about endl

    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

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    53
    WHen would you be in need to flush the stream immediately?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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.

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    53
    Alright, thanks. I'll use endl right before I cin input. Thanks again prelude

  6. #6
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Use endl 'by default'

    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 01:46 PM.

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    82
    Is there overhead associated with flushing the buffer? For example if I want to put a bunch of end lines, should i use:
    Code:
    cout << endl << endl << endl << endl;
    or
    Code:
    cout << \n\n\n << endl;

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 06:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 01:52 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM
  4. Array/Pointer question
    By Swaine777 in forum C++ Programming
    Replies: 2
    Last Post: 05-17-2003, 11:29 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21