What is "endl", what does it does?

This is a discussion on What is "endl", what does it does? within the C++ Programming forums, part of the General Programming Boards category; Could anyone tell me what really "endl" does? #include <iostream.h> int main() { char string[256]; //A nice long string cout<<"Please ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    1

    What is "endl", what does it does?

    Could anyone tell me what really "endl" does?



    #include <iostream.h>

    int main()

    {

    char string[256]; //A nice long string

    cout<<"Please enter a long string: ";

    cin.getline(string, 256, '\n'); //The user input goes into string

    cout<<"Your long string was:"<<endl<<string;

    return 0;

    }

    Thank you...

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    endl stands for endline. Pretty much starts a new line. I prefer using \n if I've already got a literal open.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    endl will also flush the output stream, '\n' will not.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    101
    Hello,

    The difference between '\n' and endl is that endl also flushes the stream. So:
    Code:
    cout << endl;
    Is the same as:
    Code:
    cout << '\n'; cout.flush();
    '\n' will give you just a newline.

    Code:
    #include <iostream.h>
    You should use the new C++ headers. So replace this with:
    Code:
    #include <iostream>
    using namespace std;
    You don't need to change anything else in your code.
    - lmov

  5. #5
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Pedro Laia, I hope you know what flushing the output stream is, if you don't, it's when you want to output something you put it in the output stream, but sometimes it's not displayed untill you flush the output stream.
    none...

  6. #6
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    I'll join the conversation here too...

    Can you give me an example of flushing ? I don't really get it

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Adding a little to ammar's response, the stream is a buffer which is, essentially, an array.

    As you may be aware, I/O is a real time-consumer where program execution is concerned. Rather than dealing with input and output on a byte-by-byte basis, the data is "buffered" - stored in the array, or stream - until it can be moved, or is forced to move, in a more efficient manner, i.e. rather than moving one byte at a time, a group of bytes are moved all at once.

    'endl', as has been noted already, forces the flushing (emptying) of the output stream. ('cin', by the way, is "tied" to 'cout' meaning that the output stream is automatically flushed when 'cin' is encountered after a 'cout'. Note that it doesn't work the other way around, though. The input stream is not automatically flushed when 'cout' is encountered following a 'cin' statement. If necessary, you can use cin.ignore() to accomplish this - which I leave to you to research. )

    On a related sidenote, you may have run across 'cerr', a kissing-cousin to 'cout'. 'cerr' is typically used when an error message must be displayed due to a program glitch that may cause the program to terminate unexpectedly. 'cerr' is not buffered for reasons that you can probably surmise. You don't want an error mesage "trapped" in a buffer that may not be displayed in the event of an impending program crash or failure.

    -Skipper

    P.S. Korhedron, 'cout' moves data to the default output device which we, almost universally, accept as the display screen (monitor). This doesn't have to be the case, though. Therefore, when the output stream is "flushed", the data is moved to wherever 'cout' is "programmed" to send it. Hope that helps a little.
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  8. #8
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Originally posted by Korhedron
    I'll join the conversation here too...

    Can you give me an example of flushing ? I don't really get it
    Once I has to flush the buffer, when I was using the getche() function which captures the key pressed by the user, and displays it, I had to use cout.flush() because otherwith it was not displayed until the end of the program(my program was only to test getche() so there was nothing else).
    none...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with in, "cout" (Each "endl"
    By andreas_nordman in forum C++ Programming
    Replies: 1
    Last Post: 05-30-2004, 08:25 AM

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