• Advertisement
Sign in to follow this  

Floats to LPCSTR

This topic is 4144 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi... I'm a newbie in C++ I have VC++ Express 2005. In my project I use MessageBox to send messages. For the text in the window of MessageBox i must pass an LPCSTR variable to the function. If I have an array of floats and i want to see them in the message how can i create an LPCSTR string from floats? Thanks!

Share this post


Link to post
Share on other sites
Advertisement
Use boost::lexical_cast, or, if you don't have Boost (which you should), use std::stringstream and insert the float as a float, then extract it as a string:


float my_float = 32.0f;
std::string my_string;
std::stringstream ss;

ss << my_float;
ss >> my_string;


Modify as needed to handle a list of floats; use my_string.c_str() to get the LPCSTR you need to pass to MessageBox.

Share this post


Link to post
Share on other sites
You can use a std::stringstream to send the floats (along with textual formatting in between them) to a stream, just like you would output stuff to std::cout. Then, you can use the .str() member function to get a std::string representing the contents of the std::stringstream, and use the .c_str() method on that to get the const char* that can be passed to MessageBox().

#include <sstream>

//...

float MyFloats[4] = { 0.0f, 1.0f, 2.0f, 3.0f };
std::stringstream ss;
ss << MyFloats[0] << ", " << MyFloats[1] << ", " << MyFloats[2] << ", " << MyFloats[3];

MessageBox(NULL, ss.str().c_str(), "Floats", MB_OK);

Additionally, you can #include <iomanip> and use functions like setw() and setprecision to format the floats more precisely.

Share this post


Link to post
Share on other sites
1)LPCSTR is just a typedef for a char *, therefore you only need to convert from float to char *

2)However, you don't need to convert to char *, you can use std::string and the stringstream family(std::stringstream, std::ostringstream, etc) to convert between float and strings

3)The std::string class has a method for returning a const char * for just such purposes

Putting that all together we get:
#include <iostream>
#include <string>
#include <sstream> // for ostringstream

using namespace std;

void printString(const char * temp)
{
cout << temp << endl;
}

string floatToString(float temp)
{
ostringstream s;
s << toString;
return s.str();
}

int main()
{
string number = floatToString(123.4);
printString(number.c_str());
return 0;
}


I can't remember off-hand, but you MAY need to perform a const_cast operation as well. What const_cast does is say: There's a function I cannot modify(in your case MessageBox), but I'm certain this function won't modify the string so forget about me saying the datatype is const and let me use the code!

see this for more:
http://www.cplusplus.com/doc/tutorial/typecasting.html

This links covers streams in general:
http://www.artima.com/cppsource/streamstrings.html

Hope that helps

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement