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
 
Ad

Advertisements

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
 
Ad

Advertisements

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.
 

Top