-
January 31st, 2008, 03:39 PM
#1
need help on strange cout behavior
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.
Code:
#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
-
January 31st, 2008, 04:04 PM
#2
Re: need help on strange cout behavior
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
-
January 31st, 2008, 04:15 PM
#3
Re: need help on strange cout behavior
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.
-
January 31st, 2008, 05:17 PM
#4
Re: need help on strange cout behavior
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.
Code:
cout << ss.rdbuf();
cout.clear();
-
December 12th, 2008, 03:15 PM
#5
Re: need help on strange cout behavior
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???
-
December 12th, 2008, 04:36 PM
#6
Re: need help on strange cout behavior
@mystmanshez
don't hijack threads! Post your questions in a new thread or you will not get answers!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|