Double to char array...
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? =/
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.
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).
C++: use the std::stringstream class from <sstream> (use it like any other stream, grab the string using the str() member function).
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);}
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]
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]
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 ;)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement