PDA

Click to See Complete Forum and Search --> : convert float to string


nabeels786
Feb 22nd, 2002, 09:51 PM
char *output="fps=";
char tmp[20];


strcat(output,itoa((int)fps,tmp,2));

that's my code, but my program always locks up at the "strcat" section. fps is a float, which i typecasted cuz of "possible loss of data"...see any problems?

I'm trying to get a string that says "FPS=<fps>", but it won't work :(

nabeels786
Feb 22nd, 2002, 10:01 PM
nevermind, i used sprintf()

parksie
Feb 24th, 2002, 05:08 PM
In pure C++:ostringstream oss;
oss << "FPS: " << fps_value;

// Do something with oss.str()

nabeels786
Feb 24th, 2002, 05:16 PM
I'm outputting it to a window i created with CreateWindow()

parksie
Feb 24th, 2002, 05:20 PM
DrawText(hDC, oss.str().c_str(), ...);...or something :)

nabeels786
Feb 24th, 2002, 05:26 PM
i think it was textout..yeah here it is:

char *output=new char[];

sprintf(output,"FPS=%f",fps);
TextOut(hDC,10,10,output,strlen(output));

The thing was i had it output every second, and it showed up for a split second then disappeared...adn reappeared every other second...confusing but yeah.

Then I outputed it in the DrawGL() section, and it did the same thing here

parksie
Feb 24th, 2002, 05:28 PM
char *output=new char[];Uh... :confused:

Not sure how that one can work...

nabeels786
Feb 24th, 2002, 05:30 PM
Hey I don't know either...I just know it works.

Should I use

char *output = (char *)malloc(255*sizeof(char));

instead? I've used that for a while without any problem, so I just stuck with it.

Or do you mean "why does it work he didn't specify a max size"?

parksie
Feb 24th, 2002, 05:34 PM
I'm just intrigued as to what size buffer you get when you don't give a size - I expect it's compiler dependent.

In C++, use new:char *buffer = new char[255];I'd still use the stringstreams one though, safer and more c++-like (probably just as fast as well, if not faster).

nabeels786
Feb 24th, 2002, 05:41 PM
I'm using VC++ 6.0

It doesn't just return 6? (FPS=30) (example)

I'll do a quick thing to check

nabeels786
Feb 24th, 2002, 05:43 PM
Yup, I just did quick test, since the number was a float, it came out as


85.00000

so strlen() was 12

parksie
Feb 25th, 2002, 11:04 AM
Before you insert the float, try something like oss.precision(2);.

nabeels786
Feb 25th, 2002, 02:07 PM
ok thanks

what's oss?

i think i'll change it to an int...i dont think the fps will go above 32000 (or whatever the max # for int is)

parksie
Feb 25th, 2002, 04:31 PM
oss is the ostringstream object from earlier.

The maximum value of an int is about 2 thousand million (4 for unsigned).

Basically, anything you can do to a stream like cout or cin, you can do to a stringstream...that's what makes them so utterly cool :D

nabeels786
Feb 25th, 2002, 04:34 PM
Ah. And that works for printf() and scanf()?

parksie
Feb 25th, 2002, 04:43 PM
I don't think they're particularly compatible with each other, but they do more or less the same thing.

Basically, as printf, scanf, sprintf, sscanf all use a format string, cout, cin, ostringstream, istringstream all use the same manipulators.

For example, if you define operator>> for your class, then it will work with a stringstream :)

nabeels786
Feb 25th, 2002, 04:47 PM
<-- C++ n00b
"Basically, as printf, scanf, sprintf, sscanf all use a format string, cout, cin, ostringstream, istringstream all use the same manipulators.

For example, if you define operator>> for your class, then it will work with a stringstream"


:confused:

parksie
Feb 25th, 2002, 04:51 PM
Ok.printf("Hello: %d\n", int_value_here);

cout << "Hello: " << int_value_here << endl;...or...void thingie(ostream &os) {
os << 56 << hex << 255 << endl;
}

void somecode() {
thingie(cout);

ostringstream oss;
thingie(oss);

cout << oss.str();
}All the streams work in the same way, and are interchangeable at the ostream / istream level.

nabeels786
Feb 26th, 2002, 01:53 PM
Ohh okay I get it...Thanks :)

parksie
Feb 26th, 2002, 01:57 PM
No problem.

When you think about it, it's far more logical than C's approach.

nabeels786
Feb 26th, 2002, 02:02 PM
yeah, you can customize it alot easier with less code.

Me think's ill use that somewhere...:)