Sign in to follow this  
chbrules

Double to char array...

Recommended Posts

chbrules    170
I'm sure this is a completely easy question to answer, but for the life of me I can't figure out. I have a char array, and I want to copy a double value into it. I figured I'd just typecast it to char, but that didn't work. I tried just using the = operator, that didn't do it. I just don't really know how, can someone help? =/

Share this post


Link to post
Share on other sites
chbrules    170
Sorry, I meant that I'm trying to copy the value into the array as a string. I can also use the std::string type if that's easier. This is for the FPS in the caption of a C++ SDL app.

Share this post


Link to post
Share on other sites
Fruny    1658
C: use the sprintf() function from <stdio.h>
C++: use the std::stringstream class from <sstream> (use it like any other stream, grab the string using the str() member function).

Share this post


Link to post
Share on other sites
chbrules    170
Alright, my program just crashes, no errors. I know it has to do with this section because when I comment out the function call it runs fine:


int last_time;
double FPS;
char cap[16];
...

void CSDL_Time::FPS_Caption()
{
//Time calcs
last_time = getTime();
FPS = 1000 / Time_Passed(last_time);

//Copy into string
sprintf (cap, "%d FPS", FPS);

//Set caption
SDL_WM_SetCaption(cap, NULL);
}

Share this post


Link to post
Share on other sites
chbrules    170
Quote:
Original post by Drew_Benton
To make sure it is not a buffer overflow try:
snprintf (cap, 15, "%d FPS", FPS);
or
_snprintf (cap, 15, "%d FPS", FPS);

Also make sure Time_Passed(last_time); does not return 0 [wink]



sprintf(cap, 15, "%d FPS", FPS);


CSDL_Time.cpp(27): error C2664: 'sprintf' : cannot convert parameter 2 from 'int' to 'const char *'


*Fixed error checking for 0 now ;)

Share this post


Link to post
Share on other sites
Zahlman    1682
%d is not meant for double values, it is meant for integer values. Logical, yeah? (It stands for 'decimal', I assume). For floating-point values (double or float) use %f (stands for 'float' I assume), %e or %g (letters adjacent to f... ok then...), which all have slightly different semantics, but I can't remember what they are (you'd be better off to just try them all). (BTW, your division is an integer division even though you assign to a double variable; what matters is that both values being divided are integers. You might want to do something about that.)

And as mentioned, check for the division by zero and the buffer size.

OR, you could do things the C++ way and save yourself lots of headaches:


void CSDL_Time::FPS_Caption() {
std::stringstream caption;
last_time = getTime();
int dt = Time_Passed(last_time);
if (dt) { caption << 1000.0 / dt << " FPS"; }
else { caption << "??? FPS"; }
SDL_WM_SetCaption(caption.str().c_str(), NULL);
}

Share this post


Link to post
Share on other sites
chbrules    170
Got it to work! ^_^

void CSDL_Time::FPS_Caption()
{
//Calc FPS
FPS = 1000.0f / (double)Time_Passed(last_time);

//Copy into string
sprintf(cap, "%f FPS", FPS);

//Set caption
SDL_WM_SetCaption(cap, NULL);

//Get time for next round
last_time = getTime();
}

Share this post


Link to post
Share on other sites
ToohrVyk    1596
Also, a somewhat more stable method of getting the fps is counting the amount of frames rendered in a certain duration, using an algorithm similar to this one:


void Frame( ) {

frames += 1;
time += timeElapsed( );

if( time > 1000.0 ) {

DisplayFPS( frames );

time -= 1000.0;
frames = 0;
}
}

Share this post


Link to post
Share on other sites
petewood    819

std::string toString(double d) {
std::stringstream ss;
ss << d;
return ss.str();
}

void CSDL_Time::FPS_Caption() {
framesPerSecond = 1000 / (double)Time_Passed(last_time);

std::string caption = toString(framesPerSecond);
caption += " FPS";

SDL_WM_SetCaption(caption.c_str(), NULL);

last_time = getTime();
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this