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
1
vote
3 answers

Using '\n' instead of endl affects output, why?

So I made an uninitialized array in C++ and tried printing the last element to see what the output would be. Every element in an uninitialized array should have the value of 0 (right?), but the output I got was something else. This is what the main…
Emil Laine
  • 41,598
  • 9
  • 101
  • 157
1
vote
2 answers

How to create endl manipulator for my class?

My class looks like: class FileOut { private: std::ofstream stream; public: FileOut(string sciezka); ~FileOut(void); friend FileOut & operator<< (FileOut & obiekt, const char* w); friend FileOut & operator<< (FileOut & obiekt,…
Danu
  • 77
  • 2
  • 10
1
vote
2 answers

Using ofstream* wrapper class with overloaded << operator on endl

C++ This is an attempt to make a class that mimics the output behavior of using the << operator of an ofstream, as far as being able to use std::endl and write strings is concerned. The class has a single data member, the ofstream pointer. The class…
CodeBricks
  • 1,771
  • 3
  • 17
  • 37
0
votes
3 answers

Need help printing a sequence, without spaces, on the same line

so here is what i'm working with. #include using namespace std; int main() { int i, h = -1; for (i = 0; i < 8; i++) { h = h + 1; cout << h << endl; } // while return 0; } // main I need my output to…
user1291612
  • 21
  • 1
  • 1
0
votes
1 answer

Is there any way to endl on cin?

I need endl on cin "name" as there may be 2 words on input "name" cout << "Enter your name: "; cin >> name; cout << "Enter your student ID: "; cin >> StuID; cout << endl << "Please enter four number."; cout << endl << "Enter the 1st…
0
votes
2 answers

seeking clarity regarding std::cout and std::endl

I am trying to get a better understanding of what happens to the buffer when we do not use std::endl after std::cout. Let us consider the following piece of C++ code - int main(int argc, char** argv) { std::cout << "Hello World!"; return…
skpro19
  • 519
  • 7
  • 10
0
votes
8 answers

C++ endl with constants

I am just trying to familiarise myself with the basics of C++ moving from Java. I just wrote this functionality abstinent program and am coming across an errortest.cpp:15: error: expected primary-expression before ‘<<’ token and I am not sure…
Jack H
  • 2,440
  • 4
  • 40
  • 63
0
votes
1 answer

Printing an array line by line but not ending with '\n' on the last line?

I am currently working on a program and one of the instructions states: "For the output file, when outputting for one command, the data should be in one line without “\n”. When outputting for a new command, a new line should be…
pancakes324
  • 11
  • 1
  • 3
0
votes
1 answer

Just starting off with C++, not registering command

I'm not really sure why "cout" and "endl" are not being recognized. Any help would be great! The error is: and the code is:
0
votes
0 answers

Comparing last word in line (added to vector each word as different element) with typing word C++

I'm making one of my firsts programs in cpp. In short, small game. Rule of that round is to choose random word from line, replace it with "...", ask for typing word and check if it is the same word as the word before replacing. I have problem only…
MichalWds
  • 13
  • 7
0
votes
0 answers

How to remove a new line for each integer?

#include using namespace std; int main() { int a, b, c; cin >> a >> b >> c; } When I compile it, every integer is asked like there is endl between them. How can I write that so the program asks for a, b and c in one line and…
Pulpit
  • 85
  • 8
0
votes
3 answers

Why can't I cout an endl in a conditional operator?

I am trying to print a comma-separated list of numbers by using a for loop, but I don't want there to be a comma after the last number, instead, I want there to be an endl. I have this code: for (int j = i; j > 0; j--) { // Should print 9, 8, 7,…
0
votes
1 answer

alternative of fflush in C++

I'm trying to find an alternative for fflush to clear the buffer in C++. I'm writing a C++ program to fork 3 children and print them in N loops but the O/P is not in correct order. Tried using cout<< flush and endl. Is there any other way to force…
Nymeria
  • 5
  • 3
0
votes
0 answers

C++ Stream buffer behavior

Does c++ automatically flush the ofstream when it is full? For example: std::ofstream OFile; OFile.open("file.txt"); for (int i = 0; i < N; ++i) { OFile << a << " " << b << " " << c << '\n';} OFile.close() Here, I did not use any manipulator to…
Eman
  • 165
  • 1
  • 1
  • 8
0
votes
1 answer

One std::endl makes three std::endl(s)?

I want to code a small command line interpreter example for a bigger program. But if I enter "1 2 3" the output is "1\n2\n3\n" and not "1 2 3\n" as I would expect. #include int main(int argc, char **argv) { while (true) { …
B. A. Sylla
  • 419
  • 3
  • 13