Solved

istream problem

Posted on 2002-05-20
4
385 Views
Last Modified: 2010-04-02
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);
}
0
Comment
Question by:khampton
  • 3
4 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 7022578
Please explain, how it doesn't work.
0
 
LVL 30

Expert Comment

by:Axter
ID: 7022590
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;
}

0
 
LVL 30

Accepted Solution

by:
Axter earned 50 total points
ID: 7022606
To get it to work the way you want it, try the following:
string cistream_to_string(istream S)
{
    string text;
    char ch;
     S.seekg(0);//Add this here
    while (true) //infinate loop
    {
         ch = S.get();
         if (S.eof()) break;
         text += ch;
    }
    return text;
}

0
 
LVL 1

Author Comment

by:khampton
ID: 7022621
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.  
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now