Jump to content
  • Advertisement
Sign in to follow this  
chbrules

Double to char array...

This topic is 4831 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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
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
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
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 this post


Link to post
Share on other sites
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!