Output Strings don't match but they do

I am working on a c++ project on the g++ compiler for linux.

The goal is to create a phone directory with entries composed of first name, last name, and phone number.

I have everything written with no compile errors, and it passes all tests but one where it uses << operator to pass and entry 'e' to an ostringstream and compares o.str() to an expected string. I have printed 'e' and o.str() to the console screen during the tests and they seem to match except a "mysterious" new line that gets inserted after o.str() is printed.

code from the my entry.cpp and tester.cpp and console output follow.

My question is two fold. Where does this new line get inserted and where/what is it that makes my test fail? Thanks in advance for any advice.

overloads from entry.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
ostream& operator<<(ostream &out, const entry &e){
	out << e.fname << ":" << e.lname << ":" << e.phone<< endl;
	return out;
}

istream& operator>>(istream &in, entry &e){
	
	string s;
	string n1,n2, p;
	getline(in,s,'\n');
	size_t found;

	found=s.find_first_of(":;,");
	while (found!=string::npos){
		s[found]=' ';
		found=s.find_first_of(":;,",found+1);
	}
	istringstream i2(s);
	i2>>n1>>n2>>p;
	e=entry(n1,n2,p);
	return in;
}


testing case code from tester.cpp

1
2
3
4
5
6
7
8
/// verifies the operator<<
void test_entry_out() {
	ostringstream o;
	entry e("Orlando","Karam","5555");
	o << e;
	cout<< "Attempting to print o.str() " << o.str() << " Attempting to print o " << o << " Attempting to print e " << e << " finished";
	CPPUNIT_ASSERT(o.str()=="Orlando:Karam:5555");
}


Output from the test cases

....Attempting to print o.str() Orlando:Karam:5555
 Attempting to print o 0xbfb02850 Attempting to print e Orlando:Karam:5555
 finishedF....


!!!FAILURES!!!
Test Results:
Run:  8   Failures: 1   Errors: 0


1) test: tester::test_entry_out (F) line: 69 tester.cpp
assertion failed
- Expression: o.str()=="Orlando:Karam:5555"

Is endl the new line character in the code below?

1
2
3
4
ostream& operator<<(ostream &out, const entry &e){
	out << e.fname << ":" << e.lname << ":" << e.phone<< endl;
	return out;
}

Agh! now I feel really stupid...

that would totally be where the new line is being inserted... and why the test failed since o.str() was capturing the endl as well.

I removed the endl to be

1
2
3
4
ostream& operator<<(ostream &out, const entry &e){
	out << e.fname << ":" << e.lname << ":" << e.phone;
	return out;
}


and it works perfectly now. Passing all tests. Thanks much Vlad!
Topic archived. No new replies allowed.