I have a basic knowledge of C++ but mostly in a limited fashion. I am building an API and I need to send the resulting string to a text box in a Modal Dialog. I will not need to manipulate the string beyond sending it to the text box.

The float can possibly get pretty long but not beyond 8 decimal places or so.

So any short and sweet helper function would be of great help.

Recommended Answers

All 7 Replies

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;
}

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();
}

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.

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.

Ok cstdio worked. I have the function looking like this:

char ftostr(float myfloat)
{
    char ans[20];

    snprintf(ans, 20, "%f", myfloat);

    return *ans;
}

But when I call the function like so:

SetDlgItemText(hwndDlgM, IDC_CONVERTION, sNum);

The third parameter sNum needs to be a const char. Any way around that?

Oops.

I call the function like this:

char sNum = ftostr(fNum);

Then the SetDlgItemText() attempts to use the converted char.

Sorry 'bout that.

OK - I came up with this:

char *ftostr(float myfloat)
{
    char ans[20];

    snprintf(ans, 20, "%f", myfloat);

    return ans;
}

And I call it like this:

char *sNum = ftostr(fNum);

SetDlgItemText(hwndDlgM, IDC_CONVERTION, sNum);

It all compiles but I ran into another problem. That is for another thread though.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.