Converting a float to char*


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: Converting a float to char*

  1. #1
    Join Date
    Apr 2004
    Location
    Pennsylvania
    Posts
    72

    Converting a float to char*

    I'm making a game in OpenGL which already has a function to print a string and now I'm trying to add a function to print a numeric value in the same way by converting the number to a character string and then sending it to the same printing function(which works). I've tried gcvt, sprintf etc. and some(including the mentioned 2) have printed the correct value initially, but they make my program freeze. so if anyone knows a good way to do this, that'd be great. The code I'm currently trying to use is:

    void print_numberf(float tX, float tY, float tSize, float fNumber)
    {
    char num[10];
    //the problem
    _gcvt(fNumber, 10, num);
    //the following line of code is fine
    print_string(tX, tY, 0, tSize, 1.0, num, FALSE);
    return;
    }

    Thanks in advance.
    -MAD Programming's
    Daniel Borgman

  2. #2
    Join Date
    Sep 2005
    Location
    istanbul / Turkey
    Posts
    133
    i typed this
    char num[10];
    _gcvt((double)1000/3, 10, num);
    cout<<num;

    and it gave 333.3333333 -->10 + 1 characters.
    i guess this overflow your num[].

    http://www.cplusplus.com/ref/cstdlib/gcvt.html
    link say use a buffer which is 8-9 character larger than ...
    Last edited by mr1yh1; 12-17-2005 at 01:41 AM.

  3. #3
    Join Date
    Dec 2003
    Posts
    3,366
    yes, thats likely the problem. A float has 15 or so digits, make num[20] to be safe.

  4. #4
    Join Date
    Nov 2003
    Posts
    4,118
    _gcvt is not a standard function so I can't vouch for it. However, a decent conversion function should be able to truncate the result so that it fits into the buffer. I would use strinsgstream for such conversions anyway -- it's safer and simpler.
    Danny Kalev

  5. #5
    Join Date
    Apr 2004
    Location
    Pennsylvania
    Posts
    72

    Yes!

    Thanks a bunch for the help, you guys. I increased the size of num and it worked, which is awesome. Thanks again.
    Daniel Borgman
    Founder, MAD Programming( MADtek )
    www.MadProgramming.biz.ly
    www.MadProgramming.byethost2.com
    Founder, Old Baby Pictures Skate( BP )
    www.obpskate.byethost12.com

  6. #6
    Join Date
    Apr 2004
    Location
    Pennsylvania
    Posts
    72
    Aw, jeez. I just tried to impliment it with a variable passed for the number and it didn't work, so I was wondering how strinsgstream works. Just in case, here's my current code again:

    void print_numberf(float tX, float tY, float tSize, float fNumber)
    {
    char num[20];
    _gcvt(fNumber, 10, num);
    print_string(tX, tY, 0, tSize, 1.0, num, FALSE);
    return;
    }

    and I also used

    sprintf(num, "%d", fNumber);

    which didn't work.
    Daniel Borgman
    Founder, MAD Programming( MADtek )
    www.MadProgramming.biz.ly
    www.MadProgramming.byethost2.com
    Founder, Old Baby Pictures Skate( BP )
    www.obpskate.byethost12.com

  7. #7
    Join Date
    Nov 2003
    Posts
    4,118
    You can find examples of stringstream usage here: http://www.devx.com/DevX/LegacyLink/9399
    Danny Kalev

  8. #8
    Join Date
    Apr 2004
    Location
    Pennsylvania
    Posts
    72
    I actually can't use sstream with Dev-C++(my compiler)
    Daniel Borgman
    Founder, MAD Programming( MADtek )
    www.MadProgramming.biz.ly
    www.MadProgramming.byethost2.com
    Founder, Old Baby Pictures Skate( BP )
    www.obpskate.byethost12.com

Similar Threads

  1. Marshalling a struct that contains a union...
    By Tim Overbay in forum .NET
    Replies: 4
    Last Post: 02-27-2002, 03:01 PM
  2. converting byte array to float
    By Curtis Nelson in forum Java
    Replies: 1
    Last Post: 03-29-2001, 11:12 AM
  3. Replies: 0
    Last Post: 11-20-2000, 05:37 AM
  4. Replies: 0
    Last Post: 11-20-2000, 05:32 AM
  5. Replies: 1
    Last Post: 10-11-2000, 12:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center