Questions tagged [endl]

For issues relating to the std::endl output-only I/O manipulator.

endl is an output-only I/O manipulator. It inserts a newline character into an output sequence os and flushes the stream. It is defined in the header ostream and part of the namespace std. The behavior is equivalent to calling os.put('\n') and then os.flush().

Minimal example:

#include <iostream>

int main()
{
    std::cout << "Hello World" << std::endl;
    return 0;
}
79 questions
4
votes
4 answers

Does anyone know the difference between endl(cout) and cout << endl?

I thought they were the same thing, but when I sent a code to an online judge (with endl(cout)) it gave me "Wrong answer" verdict, then I tried to send another with cout << endl and the judge accepted the code! Does anyone know the difference…
hinafu
  • 689
  • 2
  • 5
  • 13
4
votes
2 answers

What's the difference between std::endl and '\n'

I've read the difference between std::endl and '\n' is that std::endl flushes the buffer and '\n' doesn't. However, as far as I know stdout on linux is line-buffered anyway, so does it mean that std::cout << ... << std::endl is the same as std::cout…
eri412
  • 57
  • 1
  • 3
4
votes
2 answers

Where is endl manipulator defined

We know that endl is manipulator and internally it put '\n' to buffer and then flush up the buffer. Where is endl defined? What is endl, is it macro or function or variable or class or object? How can I define my own endl manipulator? cout <<…
3
votes
4 answers

How to overload std::cout << std::endl?

I was wondering if there was anyway I could overload std::cout << std::endl; for the endl to not only make a newline but also print a '-' where the newline is supposed to be and then print another newline. Like if I did std::cout << std::endl << '-'…
Diego Esquivel
  • 182
  • 1
  • 12
3
votes
0 answers

Why does `endl` working fine without namespace `std`?

Case 1: #include int main() { std::cout<<"Hello World"< int main() { endl(std::cout); return…
msc
  • 33,420
  • 29
  • 119
  • 214
3
votes
1 answer

cannot write a space using ofstream, but endl works fine

Okay, this may seem a simple question; but I can't seem to find an answer to it. My code is as follows: void writeFile(int grid[9][9]) { ofstream fout ("myGame2.txt"); if (fout.is_open()) { for (int i = 0; i < 9; i++) { for (int…
NateEkat
  • 33
  • 3
3
votes
1 answer

C++ overloaded << operator does not correctly output unless I include an endl

pretty interesting issue I'm having. Basically I'm overloading the insertion operator to return a string representation of my class. However, the program just terminates unless I include a std::endl. template std::ostream& operator <<…
Yasin Z
  • 31
  • 1
3
votes
1 answer

C++ cout.endl() clear the buffer, cout.flush() dont

Problem was in IDE I am using - CLion 1.2.4 gives incorrect output inside its own output window, solved. Following code gives repeating output when working with vectors, bigger then ~1000,…
tdwibar
  • 111
  • 1
  • 5
3
votes
3 answers

inserting text to a file (end line isn't entered)

I have a strange problem: I read a file into a buf and tried to run it in ssh (Linux).. my file contains: We I a so this is my buf: now I create a new file and paste the buf into this new file: FILE*nem_file_name; nem_file_name=…
Maor Cohen
  • 936
  • 2
  • 18
  • 33
2
votes
2 answers

Difference between std::endl and \n (Regarding Platform Awareness)

Before you declare this question as a duplicate of this or that, please consider that I've submitted a problem to the online judge with \n and got WA, and then with std::endl and got AC. So, I need a very specific answer to the point of platform…
OmarOthman
  • 1,718
  • 2
  • 19
  • 36
2
votes
0 answers

Why does std::endl makes this loop run forever?

I found this code on a q&a platform. #include int main() { for (int i = 0; i < 300; i++) std::cout << i << " " << i * 12345678 << std::endl; } At first sight, this seems normal but instead it runs infinitely. Results at :…
jovian
  • 121
  • 1
  • 3
2
votes
1 answer

cout and endl are undefined even though using std:: and

I am trying to enter the simple "Hello World!" code, but cout and endl are undefined. #include #include "stdafx.h" int main() { std::cout << "Hello World!" << std::endl; } It turns out the errors: "'cout': is not a member of 'std',…
2
votes
0 answers

std::endl is a template function, why can we use it without ()?

Below is the endl function from stlport. template inline basic_ostream<_CharT, _Traits>& _STLP_CALL endl(basic_ostream<_CharT, _Traits>& __os) { __os.put(__os.widen('\n')); __os.flush(); return __os; } When…
Hector.Z
  • 31
  • 3
2
votes
3 answers

C++ style Logger that supports __LINE__ macro and others

I want to make a Logger that can be used like std::cout, but I want to log some extra data like date, time, __LINE__, __func__, and __FILE__ which should be saved to the file automatically. Example ToolLogger log; log << "some data" <<…
Tom
  • 1,027
  • 2
  • 16
  • 35
2
votes
1 answer

A wrong result output in C++, when I add a statement

I try to find the min and max value in a array (size is n) use 3 * (n // 2) times comparison in once ergodic. And this is my code: #include #include //! //! @Brief:get the minmum and maxmum in 3 * ( n // 2) times…
WangYao
  • 79
  • 9