Converting a float to a string?

Started by
6 comments, last by blackpawn 17 years, 9 months ago
I'm having trouble with this. I can use the gcvt(double value, int num, char* buffer); but I want to pass a string to char* buffer. I use .c_str(), but I get a error as "invalid conversion from 'const char*' to 'char*'". How do I now convert this? Thanks,
Advertisement
You cant do that - theres a reason the c_str() returns a const char* and not a char*. Use stringstream instead. http://www.cppreference.com/cppsstream/index.html
I'm using ecvt(double value, int num, int * dec, int* sign); now, but I'll check that out. Thanks.
maybe i missed something here, but why not use atof(...)?
You can either do a const_cast if you know that the string won't be modified (<-- not correct way to go about it) or you can just copy the contents of the the const char* into a char*.
Quote:Original post by blackpawn
maybe i missed something here, but why not use atof(...)?


Isn't atof() converting a string to a float, not vise versa?
no prob. i find atof quite useful on a regular basis. nice simple api. :)
oh n/m you are going from float to string. heh. so then also check out the sprintf variants. use the ones you can pass the size of your string into so you don't screw stuff up. ;)

This topic is closed to new replies.

Advertisement