Double to char array...

Recommended Posts

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 on other sites
Copy the value into the array, or convert it to a C string?

Share on other sites
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 on other sites
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).

Awesome!

Share on other sites
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 on other sites
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]

Share on other sites
Quote:
 Original post by Drew_BentonTo 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 on other sites
%d is not a double...

Share on other sites
Quote:
 Original post by Promit%d is not a double...

I tried %f as well, same thing.

%lf should work

Share on other sites
%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 on other sites
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 on other sites
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 on other sites
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();}

Create an account

Register a new account

• Forum Statistics

• Total Topics
628354
• Total Posts
2982236

• 10
• 9
• 11
• 24
• 11