View Poll Results: Pick a operator

Voters
34. You may not vote on this poll
  • "\n"

    17 50.00%
  • <<endl;

    17 50.00%

"\n" VS <<endl;

This is a discussion on "\n" VS <<endl; within the A Brief History of Cprogramming.com forums, part of the Community Boards category; Recently the idea of which one is better has arose on both the boards in one of my posts, and ...

  1. #1
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331

    "\n" VS <<endl;

    Recently the idea of which one is better has arose on both the boards in one of my posts, and in my class. Both told me i shouldn't use "\n" but rather use <<endl;

    What is your opinion?

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    ostream _FAR & _RTLENTRY _EXPFUNC endl(ostream _FAR &); // insert newline and flush
    ostream _FAR & _RTLENTRY endl(ostream _FAR &); // insert newline and flush
    Seems like endl both breaks the line and flushes the buffer.

    Personally, I use endl in C++. Not that I know it's better, but cause I've always done so .
    Plus, it looks better with ... << endl << ...
    than ... << "\n" << ...

    I suppose you could do:

    #define endl "\n"

    or:

    #define endl "\n" << flush
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    So in other words, endl; would me much more effecient in a lengthy program and is better practice?

  4. #4
    Christian
    Join Date
    Mar 2002
    Posts
    618
    I always just use both. I use \n when the end is in quotes

    cout<<somevar<<"Text in quotes \n" ;

    and endl when it is not

    cout<<"Text in quotes"<<someVar<<endl;
    I shall call egypt the harmless dragon

    -Isaiah 30.7

  5. #5
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Originally posted by Magos
    Personally, I use endl in C++. Not that I know it's better, but cause I've always done so .
    Dito

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >So in other words, endl; would me much more effecient in a lengthy program and is better practice?
    Actually, endl would probably be less efficient per call than \n. But that kind of performance nitpicking is a bad thing in most programs, so I just use endl if I need to flush the stream.

    If I need a single newline and flush:
    std::cout<<std::endl;

    If I need two newlines and a flush:
    std::cout<<'\n'<<std::endl;

    If I need a newline, flush not needed:
    std::cout<<'\n';

    -Prelude
    My best code is written with the delete key.

  7. #7
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    How do you know when you do(n't) need to flush the line?

  8. #8
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Ride -or- Die
    How do you know when you do(n't) need to flush the line?
    I might be wrong, but:

    It is when you flush the buffer that the text actually appears on the screen. If you print:

    cout << "Hello" << flush << "World" << flush;

    "Hello" would be printed on the screen a little bit before "World", causing flicker (probably unnoticable to a human eye, but still...).

  9. #9
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    ok what mod 999'd the poll :P

  10. #10
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    What are you talking about ROD?
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  11. #11
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    thats it i'm going to start taking screenshots!

  12. #12
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    LOL!
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  13. #13
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    Screen shots? Are you implying that you don't trust the people and mods here at CP.com? Are you halucinating again? Mayeb you should talk to someone proffessionally about this ROD, wouldn't want anything to happen to you, like loosing it for example.
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  14. #14
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    ..........

  15. #15
    Banned Cgawd's Avatar
    Join Date
    Oct 2002
    Posts
    405
    endl because it not only skips to the next line but clears the buffer, keeping things nice and neat

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-06-2008, 02:43 PM
  2. differnce between endl and "\n"
    By BEN10 in forum C++ Programming
    Replies: 2
    Last Post: 07-19-2008, 11:08 AM
  3. Blank lines "\n"
    By Coding in forum C++ Programming
    Replies: 15
    Last Post: 02-18-2008, 08:56 PM
  4. tutorial confusion <<endl;
    By ikke in forum C++ Programming
    Replies: 5
    Last Post: 05-22-2003, 09:39 AM
  5. <<'\n' vs <<endl
    By Panopticon in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2003, 10:31 PM

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