Outputting Multiple Lines with cout

This is a discussion on Outputting Multiple Lines with cout within the C++ Programming forums, part of the General Programming Boards category; I'm been working my way through my C++ book ("C++ Without Fear") for a second time. Something I've noticed is ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    127

    Outputting Multiple Lines with cout

    I'm been working my way through my C++ book ("C++ Without Fear") for a second time. Something I've noticed is that in the example code, the guy often writes stuff like this:

    Code:
    cout << "Enter strings, separated by commas,";
    cout << endl << "and press ENTER: ";
    When that kind of thing could be written using less code, like this:

    Code:
    cout << "Enter strings, separated by commas,"
         << endl << "and press ENTER: ";
    Is there a benefit to writing this kind of thing in the former way?

    Thanks.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    There is no benefit. In fact, it could have just as easily been written as:
    Code:
    cout << "Enter strings, separated by commas,\nand press ENTER: ";
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    endl is different from \n

    endl: Inserts a new-line character. Additionally, for buffered streams, endl flushes the buffer (i.e. writes all unwritten characters in the buffer to the output sequence, see ostream::flush).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A problem with reading multiple lines from a file
    By edd1986 in forum C++ Programming
    Replies: 6
    Last Post: 03-19-2006, 01:26 PM
  2. Multiple structs and outputting them
    By Cstudent2121 in forum C Programming
    Replies: 7
    Last Post: 12-17-2005, 09:26 AM
  3. Multiple header files , cout undelcared probem
    By rainmanddw in forum C++ Programming
    Replies: 6
    Last Post: 11-22-2005, 10:15 PM
  4. read multiple lines from a file
    By YankeePride13 in forum C Programming
    Replies: 2
    Last Post: 11-10-2005, 10:30 PM
  5. Multiple lines on an Edit box
    By RubenJ in forum Windows Programming
    Replies: 3
    Last Post: 09-20-2001, 03:51 PM

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