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
    Posts
    35

    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

  2. #2
    Join Date
    Jan 2008
    Posts
    11

    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

  3. #3
    Join Date
    Oct 2007
    Posts
    35

    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
    Location
    West Virginia
    Posts
    7,721

    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();

  5. #5
    Join Date
    Dec 2008
    Posts
    1

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

  6. #6
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    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
  •  





Click Here to Expand Forum to Full Width

Featured