Here's a simple way to make the conversion using snprintf().
#include <iostream>
#include <cstdio>
int main()
{
char ans[20];
float myf = 123.0 + (1.0 / 3.0);
snprintf(ans, 20, "%f", myf);
std::cout << "ans->" << ans << std::endl;
return 0;
}
gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
This is a nice conversion function at will convert any type that has an << operator.
#include <string>
#include <sstream>
template<typename T>
string ConvertToString(T & value)
{
std::stringstream ss;
ss << value;
return ss.str();
}
NathanOliver
Veteran Poster
1,084 posts since Apr 2009
Reputation Points: 215
Solved Threads: 189
Ok those both look real simple, but I really need a return value of type string so I can call the function and set it equal to a variable to use in the API Dialog code.
I should have specified but I am still learning.
std::string ftostr(float myfloat)
{
char ans[20];
//float myf = 123.0 + (1.0 / 3.0);
snprintf(ans, 20, "%f", myfloat);
//std::cout << "ans->" << ans << std::endl;
return ans;
}
I tried that but my Code::Blocks compiler does not recognise snprintf. I know I am just fumbling around but at least I'm trying.
Did you include the cstdio library? If you want to return a string then cast ans as a string.
gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387