why doent endl skip line between instance calls

this code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "iostream"

class cTest
{
public:
	cTest(){
		std::cout << "test";
	}
};

void main(){
	cTest qTest1;
	cTest qTest2;
	qTest1;
	std::cout << std::endl;
	qTest2;
	system("pause");
}


outputs

testtest
press any key to continue...


when it seems to me that it ought to output

test
test
press any key to continue...


any idea why?
the constructor of cTest is called when a cTest object is created. You create a cTest object on line 12, the constructor is called outputting "test". You create another cTest object on line 13, the constructor is called outputting "test" again.

Lines 14 and 16 do nothing, as you're not acting on the object in any way.

EDIT:
To get what you want to see, you could do
1
2
3
4
5
6
int main()
{
    cTest qTest1;
    cout << endl;
    cTest qTest2;
}


also, main() must be of type int.
Last edited on
In the constructor there is no endl.

1
2
3
	cTest(){
		std::cout << "test";
	}


The constructor is called twice when objects of type cTest are being created in the statements

1
2
	cTest qTest1;
	cTest qTest2;


The constructors are called immediately each after another. So the output

testtest
press any key to continue...

is valid.
Last edited on
thanks guys, i understand now.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "iostream"

class cTest
{
public:
	void print(){
		std::cout << "test";
	}
};

int main(){
	cTest qTest1;
        cTest qTest2;
	qTest1.print();
	std::cout << std::endl;
	qTest2.print();
	system("pause");
}
Last edited on
Let me give you a cleaner help:

1
2
3
4
5
6
7
8
9
10
11
12
class cTest {
public:
    cTest() { std::cout << "test"; }
};

int main() {
    cTest q1; // THIS line prints "test".
    cTest q2; // THIS line ALSO prints "test".
    q1; // THIS line does NOTHING.
    std::cout << std::endl; //  THIS line goes down one line
    q2; // THIS line does NOTHING.
}


EDIT: Typo fixed (Accidentally wrote "lone" instead of "line")
Last edited on
much appreciated
Topic archived. No new replies allowed.