Sprintf

This is a discussion on Sprintf within the C++ Programming forums, part of the General Programming Boards category; Code: const oofloat & oofloat::operator += (const oofloat & str) { oofloat copystring(str); // copy to avoid aliasing problems oofloat ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    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!

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Is there any similar command to sprintf or itoa that can be used with a double?

  3. #3
    Registered User dizolve's Avatar
    Join Date
    Dec 2002
    Posts
    68
    Use %Lf in sprintf to handle a double.

      __               &n bsp;      ___ & nbsp;       &nb sp;       &nbsp ;    
     /\ \  __    &nbs p;           /\_ \      &nbsp ;        & nbsp;     
     \_\ \/\_\  ____     _ __\//\ \    __  __&n bsp;    __   
     /'_` \/\ \/\_ ,`\  / __`\\ \ \  /\ \/\ \  /'__`\ 
    /\ \_\ \ \ \/_/  /_/\ \_\ \\_\ \_\ \ \_/ |/\  __/ 
    \ \___,_\ \_\/\____\ \____//\____\\ \___/ \ \____\
     \/__,_ /\/_/\/____/\/___/ \/____/ \/__/   \/____/
            &n bsp; I have a BAD figlet& nbsp;addiction.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    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.

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    What function can I use to get arround this read error?

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    File one
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    File 2
    Attached Files Attached Files
    Last edited by Trauts; 01-15-2003 at 09:31 AM.

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    File 3 (open this one and compile it)
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    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.
    Joe

  10. #10
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    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!

  11. #11
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    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);
    Joe

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sprintf overflows my buffer -- why?
    By Lasston in forum C Programming
    Replies: 26
    Last Post: 06-20-2008, 05:33 PM
  2. sprintf : garbage appended
    By yeller in forum C Programming
    Replies: 9
    Last Post: 12-17-2007, 10:21 AM
  3. sprintf Wrapping, a tough one
    By AdmiralKirk in forum C++ Programming
    Replies: 3
    Last Post: 02-03-2006, 10:43 AM
  4. sprintf in C and C++
    By usu_vlsi in forum C++ Programming
    Replies: 2
    Last Post: 03-14-2005, 04:14 AM
  5. sprintf and sscanf
    By tommy69 in forum C Programming
    Replies: 10
    Last Post: 04-22-2004, 09:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21