float to char*

L

Luca

Hi all, there's a way to convert a float to a char*? I have to do this:

char* str = "Object Pos: ";
char* str1 = //convert my float value to char*;
char* s = strcat(str, str1);
DrawText(x, y, s);
 
J

John Harrison

Luca said:
Hi all, there's a way to convert a float to a char*? I have to do this:

char* str = "Object Pos: ";
char* str1 = //convert my float value to char*;
char* s = strcat(str, str1);
DrawText(x, y, s);

Simple way is to use a char array (not char*) and sprintf

char str[222];
sprintf(str, "Object Pos: %g", floatValue);
DrawText(x, y, str);

Of course it is VITAL that your array is big enough. And for this reason it
really better to do things that safer way and use std::string and
std::eek:stringstream.

#include <string>
#include <sstream>

std::eek:streamstream str;
str << "Object Pos: " << floatValue;
DrawText(x, y, str.str().c_str());

john
 
L

Luca

Thanks a lot.

Luca.


John Harrison said:
Luca said:
Hi all, there's a way to convert a float to a char*? I have to do this:

char* str = "Object Pos: ";
char* str1 = //convert my float value to char*;
char* s = strcat(str, str1);
DrawText(x, y, s);

Simple way is to use a char array (not char*) and sprintf

char str[222];
sprintf(str, "Object Pos: %g", floatValue);
DrawText(x, y, str);

Of course it is VITAL that your array is big enough. And for this reason
it
really better to do things that safer way and use std::string and
std::eek:stringstream.

#include <string>
#include <sstream>

std::eek:streamstream str;
str << "Object Pos: " << floatValue;
DrawText(x, y, str.str().c_str());

john
 
H

Howard

John Harrison said:
Luca said:
Hi all, there's a way to convert a float to a char*? I have to do this:

char* str = "Object Pos: ";
char* str1 = //convert my float value to char*;
char* s = strcat(str, str1);
DrawText(x, y, s);

Simple way is to use a char array (not char*) and sprintf

char str[222];
sprintf(str, "Object Pos: %g", floatValue);
DrawText(x, y, str);

Of course it is VITAL that your array is big enough. And for this reason
it
really better to do things that safer way and use std::string and
std::eek:stringstream.

#include <string>
#include <sstream>

std::eek:streamstream str;

you mean...

std::eek:stringstream str;

? (or was it ostrstream, I forget)
 
J

John Harrison

Howard said:
John Harrison said:
Luca said:
Hi all, there's a way to convert a float to a char*? I have to do this:

char* str = "Object Pos: ";
char* str1 = //convert my float value to char*;
char* s = strcat(str, str1);
DrawText(x, y, s);

Simple way is to use a char array (not char*) and sprintf

char str[222];
sprintf(str, "Object Pos: %g", floatValue);
DrawText(x, y, str);

Of course it is VITAL that your array is big enough. And for this reason
it
really better to do things that safer way and use std::string and
std::eek:stringstream.

#include <string>
#include <sstream>

std::eek:streamstream str;

you mean...

std::eek:stringstream str;

Yes, thanks for the correction.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,165
Latest member
JavierBrak
Top