CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2007

    need help on strange cout behavior


    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.

    using namespace std;
    int main()
    	stringstream ss;
    	cout << ss.rdbuf();
    	string s = "abcdefg";
    	cout << s; // nothing comes out on screen.
    	cout << "123"; // nothing.


  2. #2
    Join Date
    Jan 2008

    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.


  3. #3
    Join Date
    Oct 2007

    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.

  4. #4
    Join Date
    Aug 2000
    West Virginia

    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.

    cout << ss.rdbuf();

  5. #5
    Join Date
    Dec 2008

    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;



    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???

  6. #6
    Join Date
    May 2001

    Re: need help on strange cout behavior

    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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)