?
Solved

istream problem

Posted on 2002-05-20
4
Medium Priority
?
395 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

752 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