new line

This is a discussion on new line within the C++ Programming forums, part of the General Programming Boards category; whats the difference between /n and endl?...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    28

    new line

    whats the difference between /n and endl?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    nothing

    all it does is prevent extra shift keying
    cout << somevariable << endl;
    instead of
    cout << somevariable << "\n";

  3. #3
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    \n is an escape sequence.

    And endl does the same thing but its a standatd strem thingie.

    I really dont know the technicalities. Im sure someone does.

    example
    Code:
    cout << "Line one\nLine two";
    cout << "Line one" << endl << "Line Two";
    What is C++?

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    ok, there is a difference....

    endl is a global function which returns '\n'
    << is an overloaded operator which tells the ostream class cout how to handle the data type that follows

  5. #5
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    i believe endl also calls flush to flush the stream
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    28

    .

    ok....when did a stream come into the picture?

  7. #7
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    endl is a manipulator in the ostream class. When inserted into an output stream it inserts a newline character then flushes the stream. It is cheaper to just use "\n" then to use endl because flushing the stream unnecessarily can slow down your program.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    a stream came into the picutre you you put
    #include <iostream>
    in your program

  9. #9
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    a stream came into the picutre you you put
    #include <iostream>
    in your program
    Actually I believe NiVaG was referring to your seemingly irrelevant explanation of the insertion operator.

  10. #10
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Quote Originally Posted by misplaced
    cout << somevariable << "\n";
    when using \n be sure to use single quotes to insert the character. When you use double quotes it creates a null terminated c-string which will cost you an extra sizeof(char)

  11. #11
    Registered User
    Join Date
    May 2002
    Posts
    66
    Here is a sample implementation of endl:

    Code:
    template<class _Elem, class _Traits> inline
    	basic_ostream<_Elem, _Traits>&
    		__cdecl endl(basic_ostream<_Elem, _Traits>& _Ostr)
    	{	// insert newline and flush stream
    	_Ostr.put(_Ostr.widen('\n'));
    	_Ostr.flush();
    	return (_Ostr);
    	}
    When talking about endl, you are implying streams as endl is an operator to ostream (a stream class). So there is definitely relevance there, adn as stated before endl does a flush() after "\n".

  12. #12
    Registered User Elhaz's Avatar
    Join Date
    Sep 2004
    Posts
    32
    Sometimes when I use cin.get() to pause the program before ending (keep the console open long enough to read it) it doesn't work by itself and I have to precede it with cin.ignore(). I understand that this is because there is still a \n in the input buffer....maybe? Would using endl, since it flushes the stream, make it so that I wouldn't have to enter cin.ignore()? Or are they completely unrelated things?

  13. #13
    Registered User
    Join Date
    Jul 2004
    Posts
    60
    Quote Originally Posted by Elhaz
    Sometimes when I use cin.get() to pause the program before ending (keep the console open long enough to read it) it doesn't work by itself and I have to precede it with cin.ignore(). I understand that this is because there is still a \n in the input buffer....maybe? Would using endl, since it flushes the stream, make it so that I wouldn't have to enter cin.ignore()? Or are they completely unrelated things?
    Couldn't you use cout.flush() and cin.flush() before cin.get()?
    Child who knows C++
    Using Borland C/C++ Compiler 5.5 (Command Line Version)

  14. #14
    Registered User Elhaz's Avatar
    Join Date
    Sep 2004
    Posts
    32
    Couldn't you use cout.flush() and cin.flush() before cin.get()?
    I don' know. That's the first time I've seen those. I'll check them out. Thanks.

  15. #15
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Since endl flushes the output stream, it wouldn't help much in clearing the input stream.

    I believe flush is only defined for output streams, so it wouldn't work for cin.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to do encryption in C
    By sankarv in forum C Programming
    Replies: 33
    Last Post: 12-28-2010, 11:01 AM
  2. Reading a file line by line
    By Raskalnikov in forum C Programming
    Replies: 8
    Last Post: 03-19-2009, 12:44 AM
  3. Pointer and Polymorphism help.
    By Skyy in forum C++ Programming
    Replies: 29
    Last Post: 12-18-2008, 09:17 PM
  4. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 04:13 PM
  5. Finding carriage returns (\c) in a line
    By JizJizJiz in forum C++ Programming
    Replies: 37
    Last Post: 07-19-2006, 06:44 PM

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