Flush & Endl problem

Dear all,

I'm new in programming and I'm just testing Arrays with pointers.

Problem:
1. If I choose endl then it pick 2 element of the array
2. If I choose flush then it pick correctly from 1st element of the array.

I want to know why is it so.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <conio.h>
using namespace std;

int main() {

	int NUM[5] = { 1, 2, 3, 4, 5 };
	int *ptr;
	ptr = NUM;

	for (int i = 0; i < 5; i++) {
		cout << "Pointer " << *ptr << " " << endl;  
		ptr++;
	}

	return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <conio.h>
using namespace std;

int main() {

	int NUM[5] = { 1, 2, 3, 4, 5 };
	int *ptr;
	ptr = NUM;

	for (int i = 0; i < 5; i++) {
		cout << "Pointer " << *ptr << " " << flush;  
		ptr++;
	}

	return 0;
}
Could you explain what "pick 2 element" and "pick correctly from 1st element" mean?
I don't understand the question. Perhaps you could clarify.

The output of the first is:
Pointer 1
Pointer 2
Pointer 3
Pointer 4
Pointer 5

and the output of the second is
Pointer 1 Pointer 2 Pointer 3 Pointer 4 Pointer 5


The only visible difference is that the first is shown on separate lines, while the second is all on a single line.

That is consistent with the intention of endl, which is to first output a '\n' character and then flush the stream.

I think it is my compiler problem
Im using eclipse.

thanks for consideration.
Why do you think it is a compiler problem?

Chervil's explanation should apply to any standards compliant compiler.
Topic archived. No new replies allowed.