Is there any real difference in usingendland\n?

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

Printable View

- 09-08-2002Chewbaccaendl 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? - 09-08-2002Stoned_Coder
Yes there is a small difference.

'\n' does not flush the stream automatically.

endl does flush the stream.

otherwise they are pretty interchangeable. - 09-08-2002Chewbacca
Ok thankyou. I'll wait till I get further into the book to find out about the differences.

- 09-08-2002indigo0086
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;

- 09-08-2002endo
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?

- 09-08-2002indigo0086
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.