endl or \n ?

This is a discussion on endl or \n ? within the C++ Programming forums, part of the General Programming Boards category; Well i find out these: Code: #include <iostream> using namespace std; int main() { char a,b; cout << "Type a ...

  1. #31
    Registered User
    Join Date
    Aug 2007
    Posts
    66
    Well i find out these:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        char a,b;
        
        cout << "Type a char!" << endl;
        cin >> a;
        cout << "Type another char!" << endl;
        cin >> b ;
        cout << "You printed " << a << b << endl;
        system("PAUSE");
        return 0;
    }
    So, endl1 flushes the output! Great!

    This can be done without system("PAUSE"); using cin.ignore(); just after cin. Pretty cool, ah ?

  2. #32
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you actually want to flush the standard input stream (cin/stdin), you shouldn't use fflush(stdin) - because it doesn't actually do what you want... It may work under some circumstances, but not others.

    See: http://faq.cprogramming.com/cgi-bin/...&id=1043284392

    --
    Mats

  3. #33
    Registered User
    Join Date
    Aug 2007
    Posts
    66
    Yes, I used fflush(sdin) due to my C little experience.

  4. #34
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If the program "mysteriously" doesn't stop for input it has absolutely nothing to do with flushing "cout", "endl" and "\n" and everything with input stream containing characters from the previous input statements.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #35
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> Can you give me a solution with endl ? Because as you said before it flushes the output buffer. So lets see then.

    What was wrong with my earlier example?

    Make sure you understand the concepts that are being explained to you before continuing to ask for examples. If you understood the concept, you wouldn't need any further examples.

  6. #36
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    In particular, make sure you understand the difference between input and output. We already corrected you on the misconception that flush flushes the input buffer. It doesn't. It only flushes the output buffer.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #37
    Registered User
    Join Date
    Aug 2007
    Posts
    66
    I see. I guess that I have not figure out the meaning of output/input buffers.

  8. #38
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    Sesame Street actually had a great analogy for what an output buffer is (though they were using it in a different context) in the form of a Burt & Ernie sketch.

    Burt was Ernie's dad and Ernie was beating his drum. Burt came in and complained that Ernie needed to take the garbage out, so Ernie picked up the single piece of garbage that was present at the moment and took it all the way to the garbage dump. This was repeated a couple times, and then Ernie got the idea to turn his drum upside down and use it as a trash can so he wouldn't have to make so many *LONG* trips to the garbage dump.

    The trash can here would be a buffer, and that *LONG* trip to the garbage dump would be a good way to explain how much extra time it takes to actually output something on the screen. Compared to shuffling a few bits around in memory, activating the hardware to display something on the screen or write something to a disk file takes a LOT longer.

    Instead of making several long trips with a little data each time, the buffer is a way to collect some amount of the data to take all at once. You may have times when you need just a single character to be displayed, like a prompt, so you can use endl to flush the output buffer (tell Ernie to make the trip with a single candy wrapper!). Most of the time letting the system determine when to flush the buffer will result in improved performance.

    An input buffer would be the same thing, except instead of collecting data to display on the screen or write to disk, it would be collecting input from the user (what you type). If Ernie was taking things to be recycled for money instead of just to the garbage dump, he would put the money he gets into his pocket to bring home (his pants pocket being the buffer), with the alternative being he would bring back each individual coin and dollar bill in a separate trip.
    abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "

  9. #39
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Or, to remove the misleading connection to output from the analogy, not having an input buffer is like going to the bank every time you need some cash and retrieving exactly the amount you need; using an input buffer means retrieving a larger amount in advance and only going back when you run out.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

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

Similar Threads

  1. endl or \n ?
    By plain in forum C++ Programming
    Replies: 5
    Last Post: 09-01-2006, 01:50 PM
  2. endl - \n
    By Hugo716 in forum C++ Programming
    Replies: 8
    Last Post: 05-25-2006, 02:33 PM
  3. Endl - Undeclared identifier.
    By MipZhaP in forum C++ Programming
    Replies: 9
    Last Post: 03-03-2005, 10:01 AM
  4. endl vs \n
    By Chewbacca in forum C++ Programming
    Replies: 5
    Last Post: 09-08-2002, 12:42 PM
  5. "\n" or endl
    By itld in forum C++ Programming
    Replies: 2
    Last Post: 11-02-2001, 12:05 AM

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