-
Sprintf
Code:
const oofloat & oofloat::operator += (const oofloat & str)
{
oofloat copystring(str); // copy to avoid aliasing problems
oofloat size = (atof(CString) + str.converttofloat());
size = size.length();
int newLength = size.converttofloat(); // size of totaled number
int lastLocation = length(); // index of '\0'
// check to see if local buffer not big enough
if (newLength >= Capacity)
{
Capacity = newLength + 1;
char * newBuffer = new char[Capacity];
strcpy(newBuffer,CString); // copy into new buffer
delete [] CString; // delete old string
CString = newBuffer;
}
// now add str (copystring) to the integer value of CString
//itoa( (atof(CString) + copystring.converttofloat()), CString, 10 );
sprintf ( CString, "%f", (atof(CString) + copystring.converttofloat()) );
return *this;
}
At this line: //itoa( (atof(CString) + copystring.converttofloat()), CString, 10 );
That is what I was doing when I was doing a class similar to this, only with an integer. Now, I can't use "ftoa" because it doesn't exist, and the other functions I could find don't do what I want. However, another code sample someone gave me (used in my integer class) used sprintf. I tried to use it in
sprintf ( CString, "%f", (atof(CString) + copystring.converttofloat
()) );
so that I could store the double as a string. That works, but it gives me a memory violation. And on the other parts of the program; I have code like this:
Code:
const oofloat & oofloat::operator += ( double rhs )
{
double hold = atof(CString) + rhs;
char s[10];
sprintf ( s, "%f", hold );
return operator= ( s );
}
I need a replacement for sprintf there, too.
Thank you for the help!
-
Is there any similar command to sprintf or itoa that can be used with a double?
-
Use %Lf in sprintf to handle a double.
-
sprintf ( CString, "%Lf", (atof(CString) + copystring.converttofloat()) );
CString is a dynamically allocated variable.
an itoa version of it works fine, only it doesn't end as a float
I still get a write error, so I guess I can't put it into CString like that.
-
What function can I use to get arround this read error?
-
-
-
File 3 (open this one and compile it)
-
If you want an alternative to sprintf replace
sprintf ( CString, "%Lf", (atof(CString) + copystring.converttofloat()) );
with
stringstream ss;
ss << atof(CString) + copystring.converttofloat();
ss >> CString;
including sstream.
Or else you can keep sprintf and try different format specifiers ('g' seemed to work) and shouldn't converttofloat return a float/double and you need to read up on proper use of header files amongst other things.
-
I'm not getting any errors except the run time one.
It is getting a read memory error, I think. itoa worked on my integer version of this.
Sprintf would work if I put it into a buffer and then transfered that to CString, but I don't know a good way to do that.
I should reallocate space for it, correct?
Thanks!
-
You're getting a memory error because by using "%Lf" as a format specifier you are generating a string that is too long. This demonstrates -
Code:
double d = 1.;
char c[3];
sprintf(c,"%lf",d);