Double to char array...

Started by
13 comments, last by Qw3r7yU10p! 19 years ago
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? =/
-Conrad
Advertisement
Copy the value into the array, or convert it to a C string?
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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.
-Conrad
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).
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Awesome!
-Conrad
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);}
-Conrad
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]
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 ;)
-Conrad
%d is not a double...
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
Quote:Original post by Promit
%d is not a double...


I tried %f as well, same thing.
-Conrad

This topic is closed to new replies.

Advertisement