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?
Printable View
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?
Yes there is a small difference.
'\n' does not flush the stream automatically.
endl does flush the stream.
otherwise they are pretty interchangeable.
Ok thankyou. I'll wait till I get further into the book to find out about the differences.
I noticed that in one particular program I did for school
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.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;
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?
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.