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
Solved

istream problem

Posted on 2002-05-20
4
392 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

828 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