What's wrong with this code?

std:: cout << result << endl;

Thank you
endl should be std::endl.
closed account (3pj6b7Xj)
1
2
3
4
5
6
7
8
#include <iostream>

in main()
{
     int result = 10;
     std::cout << result << std::endl;
     return 0;
}


By the way, endl is a manipulator...I could have just wrote.

std::cout << result;

But really what endl does is determine how a new line is inserted based on the current environment, mostly for portability. If I left out the endl; I would have to use "\n" or even "\n\r" and that may not even work if you compile the program on another os...using endl; saves you from this madness.
Last edited on
Mrfaosfx, that makes no real sense at all.

First of all, it should be: int main()
Second, \n is just a short way to write char(10), which all OS's support. If an OS doesn't support Unicode or even ASCII, I seriously doubt the usefulness of that OS. On top of that, if you ever find such an OS, please tell me.

Std::endl is a manipulator that adds a new character and, for all buffered streams, it will flush it, too. This means that it will add a \n, and if there are characters that have not yet been written, but are buffered to, for example, cout, it will print them.

http://www.cplusplus.com/reference/iostream/manipulators/endl/
Last edited on
closed account (3pj6b7Xj)
Thats not what my C++ primer plus book says, it sounds similar to yours but definately not the same, we may have different views as to how we learned what endl is as for int main() that was a typo.
endl not only print a newline but also flush out to the stream immediately. By putting "\n" it maybe buffered first before later on then flush out to the stream.

So if you output millions of lines to stream, then endl and "\n" can be a difference in terms of performance.
Topic archived. No new replies allowed.