Solved

istream problem

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

803 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