0

what's the use of endl in the given program

#include<iostream> using namespace std; int main () { int a,b; int sum=a+b; cout<<"enter a number"; cin>>a; cout<<"enter another number"; cin b; cout<<" sum is"<<sum<<endl;/* here what's the use*/ return 0; }

25th Jul 2016, 11:46 PM
pravesh
pravesh - avatar
4 odpowiedzi
+ 1
std::endl moves to the next line, the same thing as \n does. The only difference being that endl clears up the buffer. So if you're dealing with a lot of different user input, it would be best to use endl, otherwise \n provides a shorter alternative.
26th Jul 2016, 12:16 AM
Cohen Creber
Cohen Creber - avatar
+ 1
hi Cohen, I don't get clear the buffer. will you furthermore explain me.
26th Jul 2016, 4:25 AM
pravesh
pravesh - avatar
+ 1
It's easier to explain if you watched a video to visualise it for you, but the buffer is simply a stream which queues up user input, and input at the front of that stream is used first. I'll give an example: cin >> str1; User enters "Hello World" cin >> str2 cout << str1 << str2 The output of this would be HelloWorld (mostly because cin only take input up to a space, hence why getline() is used with strings, but regardless). When it comes across this space, it declares it as the end of the input, making "Hello" to be str1, with "World" still in the buffer. When it comes to inputing str2, it uses the next item in the buffer, which is the string "World"! endl clears this buffer so things like this do not happen. There are also dedicated functions to clearing the buffer without using endl. If this is still confusing, a Youtube video will help you out miles, as it is something that is better seen.
26th Jul 2016, 7:21 AM
Cohen Creber
Cohen Creber - avatar
0
it will leave a line after printing the answer and cursor will blink on next line.
26th Jul 2016, 5:29 PM
Anirudh Gupta
Anirudh Gupta - avatar