Re: ? How to clear ostringstream buffer

M

Mark Warren

Ericcson said:
Hi.
----------------------------------------
#include <fstream>
#include <stdlib.h>
#include <sstream>
#include <iostream>

using namespace std ;
int main() {
ostringstream myO;
myO << 12;
cout << "myO = " << myO.str() <<endl;
myO << 8;
cout << "myO =" << myO.str() <<endl;
return 0;
}
--------------

I get :
12
128

I would like to get :
12
8
i.e to clear the myO buffer before filling it with "8".
I don't find how. Could someone help me ?

TIA
Ericsson

PS : the all strory is that I try to convert an integer to a string . I
tried itoa(12,s,10) and sprintf(s,"%d",12) with or without c_str() and only
got compilation errors.

Use myO.str(""). A non-obvious use and IMHO poor design, a function should
only have one use.

Mark
 
E

Ericcson

Many thanks, Mark. It works OK.

Ericsson.

Mark Warren said:
Use myO.str(""). A non-obvious use and IMHO poor design, a function should
only have one use.

Mark
 
T

tom_usenet

Use myO.str(""). A non-obvious use and IMHO poor design, a function should
only have one use.

That function does only have one use (to set the string held by the
ss). Or are you against overloading?

Tom
 
S

Sam Holden

That function does only have one use (to set the string held by the
ss). Or are you against overloading?

It also returns the current string (an example of such usage was in the
OP...).

Hence one function, two uses.

Overloading a function name is fine, but the functions should do the
similar actions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top