Link to home
Start Free TrialLog in
Avatar of khampton
khampton

asked on

istream problem

I can't seem to fix this bug -- it uses and istream.  The first call works fine but the 2nd call does not: (Thanks in advance for your assistance!)

// UMBox2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <windows.h>
#include <string>
#include <sstream>
using namespace std;


//prototypes:
string cistream_to_string(istream MyStream);
long FullUMBox(HWND hwnd,istream MsgStm,const string Title,UINT style);


int main()
{
     long ret;
     stringstream S;
     S << "line one" << endl << "line two" << endl << 123.456;
     //this displays the correct result:
     ret=FullUMBox(NULL,S,"TEST1",MB_OK);
     //this doesn't work:
     ret=FullUMBox(NULL,S,"TEST2",MB_OK);
     return 0;
}


string cistream_to_string(istream S)
{
     string text;
     char ch;
     while (true) //infinate loop
     {
          ch = S.get();
          if (S.eof()) break;
          text += ch;
     }
     return text;
}


long FullUMBox(HWND hwnd,istream MsgStm,const string Title,UINT style)
{
  string text = cistream_to_string(MsgStm);
  return MessageBox(hwnd,text.c_str(),Title.c_str(),style);
}
Avatar of Axter
Axter
Flag of United States of America image

Please explain, how it doesn't work.
Try the following:

int main()
{
    long ret;
    stringstream S;
    S << "line one" << endl << "line two" << endl << 123.456;
    //this displays the correct result:
    ret=FullUMBox(NULL,S,"TEST1",MB_OK);

    //this wors know
    S << "line one again" << endl << "line two again" << endl << 123.456;
    ret=FullUMBox(NULL,S,"TEST2",MB_OK);
    return 0;
}

ASKER CERTIFIED SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of khampton
khampton

ASKER

Thanks -- I did that with an earlier version but it didn't work because I was passing byref.  Now that I'm passing byval, it seems to works fine.