endl vs \n

This is a discussion on endl vs \n within the C++ Programming forums, part of the General Programming Boards category; Is there any real difference in using endl and \n ? It seems they do the same thing but are ...

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    12

    endl vs \n

    Is there any real difference in using endl and \n?

    It seems they do the same thing but are there situations one is best used over the other?

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Yes there is a small difference.

    '\n' does not flush the stream automatically.

    endl does flush the stream.

    otherwise they are pretty interchangeable.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    12
    Ok thankyou. I'll wait till I get further into the book to find out about the differences.

  4. #4
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    I noticed that in one particular program I did for school
    Code:
    using namespace std;
    int main()
    {
          float numOne, numTwo, numThree, numFour, numFive;
          int convOne, convTwo, convThree, convFour, convFive;  //represent decimals converted into integers
          int sum;                                              //represents sum of all integers
    
          cout << "Follow the directions on the screen\n\n";
          cout << "Please enter the first decimal number and press enter: ";
    
          cin >> numOne;
          cout << "\nPlease enter the second decimal number and press enter: ";
          cin >> numTwo;
          cout << "\nPlease enter the third decimal number and press enter: ";
          cin >> numThree;
          cout << "\nPlease enter the fourth decimal number and press enter: ";
          cin >> numFour;
          cout << "\nPlease enter the last decimal number and press enter: ";
          cin >> numFive;
    
          cout << "The numbers you entered were as follows:\n";
          cout << "1:\t" << numOne << endl;
          cout << "2:\t" << numTwo << endl;
          cout << "3:\t" << numThree << endl;
          cout << "4:\t" << numFour << endl;
    line 23: cout << "5:\t" << numOne << "\n";    // endl would not allow further execution
    
          cout << "Now rounding numbers to nearest integer........\n\n";
    
          convOne = (int) (numOne + 0.5);                //converting the decimals to
          convTwo = (int) (numTwo + 0.5);                //nearest integer
          convThree = (int) (numThree + 0.5);
          convFour = (int) (numFour + 0.5);
          convFive = (int) (numFive + 0.5);
    
          sum = convOne + convTwo + convThree + convFour + convFive;  //adds the integers
    
          cout << "The sum of these integers is: " << sum << endl;
          cout << "The average of these integers is: " << sum / 5 << endl;
    
    
    
    
    
          cin.ignore(10000, '\n');    //pauses program to keep
          cin.get();                  //from early execution
          return 0;
    the first time I made the prog, i had endl on line 23 instead of newline and efter it got to line 23 it would not continue. I changed it to "\n" just to see if there was a difference and the program continued as it was intended. I don't get it that much though, I use \n all of the time anyway.

  5. #5
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Cant see why that caused a problem, I bet you corrected the error without meaning to. Have you tried chaging it back to an endl?
    Couldn't think of anything interesting, cool or funny - sorry.

  6. #6
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    yeah, did the same thing. I forgot to mention that it wasn't 23 but the line after that didn't continue aftarwards, it displayed the rounded first number, and then stopped, wonder why it continued even when the line I changed was before it.

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, 02:50 PM
  2. endl - \n
    By Hugo716 in forum C++ Programming
    Replies: 8
    Last Post: 05-25-2006, 03:33 PM
  3. endl
    By Mick D in forum C++ Programming
    Replies: 7
    Last Post: 10-20-2005, 01:54 AM
  4. Endl - Undeclared identifier.
    By MipZhaP in forum C++ Programming
    Replies: 9
    Last Post: 03-03-2005, 11:01 AM
  5. "\n" or endl
    By itld in forum C++ Programming
    Replies: 2
    Last Post: 11-02-2001, 01:05 AM

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