-
convert float to string
Code:
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 :(
-
nevermind, i used sprintf()
-
In pure C++:
Code:
ostringstream oss;
oss << "FPS: " << fps_value;
// Do something with oss.str()
-
I'm outputting it to a window i created with CreateWindow()
-
Code:
DrawText(hDC, oss.str().c_str(), ...);
...or something :)
-
i think it was textout..yeah here it is:
Code:
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
-
Code:
char *output=new char[];
Uh... :confused:
Not sure how that one can work...
-
Hey I don't know either...I just know it works.
Should I use
Code:
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"?
-
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:
Code:
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).
-
I'm using VC++ 6.0
It doesn't just return 6? (FPS=30) (example)
I'll do a quick thing to check
-
Yup, I just did quick test, since the number was a float, it came out as
85.00000
so strlen() was 12
-
Before you insert the float, try something like oss.precision(2);.
-
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)
-
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
-
Ah. And that works for printf() and scanf()?
-
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 :)
-
<-- 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:
-
Ok.
Code:
printf("Hello: %d\n", int_value_here);
cout << "Hello: " << int_value_here << endl;
...or...
Code:
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.
-
Ohh okay I get it...Thanks :)
-
No problem.
When you think about it, it's far more logical than C's approach.
-
yeah, you can customize it alot easier with less code.
Me think's ill use that somewhere...:)