Click to See Complete Forum and Search --> : need help on strange cout behavior


SamWo123
January 31st, 2008, 02:39 PM
Hi

cout in the code below stops outputting anything after I copy buffer from empty sstringstream to it.
I dont quite understand why it happens.
I'll appreciate if you could help explain this.


Is there a better way to check stringstream before using rdbuf?
I was going to try if(ss.tellp() != ios::beg) but it caused compiled time error.



#include<iostream>
#include<string>
#include<sstream>
using namespace std;

int main()
{
stringstream ss;

cout << ss.rdbuf();
string s = "abcdefg";
cout << s; // nothing comes out on screen.
cout << "123"; // nothing.

}




Regards,
Sam

chemicalNova
January 31st, 2008, 03:04 PM
Do you understand what the rdbuf member does?

Looks to me, like it interprets it as setting the buffer associated with it.. and when you use cout, you're writing to some random place in memory.. not the console.

chem

SamWo123
January 31st, 2008, 03:15 PM
My understanding is that rdbuf() allow access to the underlying streambuf from one iostream to another, alowing cout to read from streambuf of ss.

But I dont understand why it has problem in the attached code when streambuf in ss is empty.

Philip Nicoletti
January 31st, 2008, 04:17 PM
1) I am not sure what the correct behavior is.

2) My guess is that when the stringstream is empty, the
"cout << ss.rdbuf();" FAILS (not just outputs nothing).
Once the stream's state becomes bad, further use of the
stream fails.

3) If this is the case, clear couts internal flags.


cout << ss.rdbuf();

cout.clear();

mystmanshez
December 12th, 2008, 02:15 PM
hey this is my code.. the classes are returning correct values.. but look at the output!!!!! i cant fix it.. ive tried everything.. it works perfectly in visual studio but it messes up this way in unix. and since i have to submit this in unix i need help.

part of the code:

cout << setw(25)<<"Name" << setw(20)<< "Phone Number" << setw(15) << "Date of Birth" << endl << endl;
int i;
for (i = 0;(i<y);i++)
{
p = b[i];
cout << setw(25) << p.get_person().getLastName() + ", " + p.get_person().getFirstName() + " " + p.get_person().getMiddleName();
cout << setw(20) << p.getphone() ;
cout << p.get_date().getDayofweek() << ", ";
if (p.get_date().getMonth()==1)
cout << "January";
else if (p.get_date().getMonth() == 2)
cout << "February";
else if (p.get_date().getMonth()==3)
cout << "March";
else if (p.get_date().getMonth()==4)
cout << "April";
else if (p.get_date().getMonth()==5)
cout << "May";
else if (p.get_date().getMonth()==6)
cout << "June";
else if (p.get_date().getMonth()==7)
cout << "July";
else if (p.get_date().getMonth()==8)
cout << "August";
else if (p.get_date().getMonth()==9)
cout << "September";
else if (p.get_date().getMonth()==10)
cout << "October";
else if (p.get_date().getMonth()==11)
cout << "November";
else if (p.get_date().getMonth()==12)
cout << "December";
cout << " " << p.get_date().getDay() << ", " << p.get_date().getYear() << endl;

}


output:

Name Phone Number Date of Birth

Tuesday, October 12, 197055-5555

_________________________________________________________________


how is there no name?? and no comma??? and why the hell is the date before the number???

Richard.J
December 12th, 2008, 03:36 PM
@mystmanshez
don't hijack threads! Post your questions in a new thread or you will not get answers!