void WindowManager::msgbox(const char * msg, ...) {
// the argument list variable
va_list argv;
va_start( argv, msg );
float test = va_arg( argv, float );
char foo[50];
sprintf(foo, "%f", test);
MessageBox(NULL, foo, "ere", MB_OK);
va_end( argv );
}
variable arguments driving me insane
ok this seems like it should have a pretty simple answer. basically i''m writing a simple win specific error message printer that can follow the same rules formatting rules as printf (pass a string with %d, %f, etc and then a variable list of arguments).
everything is working fine except the bitch isn''t currently able to correctly read in floats, even when i strip it down to the bare bones.
the problem: I pass the following:
WindowManager::msgbox("foo", 1.2f);
and the error box prints out 2.000000
different floats passed in result in other, more bezerko results. it seems like it''s reading in the info at a wrong offset or something.
here''s the bare function code that i''ve stripped it down to in an attempt to isolate the problem. this is the actual code generating the problem:
Try it this way
The format specifier should mirror the number of varied arguments, just like with sprintf et al.
Also note that on Win32 wsprintf doesn't handle floats. I'm not certain about wvsprintf.
[edited by - lessbread on July 27, 2002 1:19:08 AM]
void WindowManager::msgbox(const char * format, ...) { // the argument list variable va_list argv; char foo[50]; va_start( argv, format ); vsprintf(foo, format, argv); // note the 'v' va_end( argv ); MessageBox(NULL, foo, "ere", MB_OK); }
The format specifier should mirror the number of varied arguments, just like with sprintf et al.
Also note that on Win32 wsprintf doesn't handle floats. I'm not certain about wvsprintf.
[edited by - lessbread on July 27, 2002 1:19:08 AM]
that prints even more bezerko info than the other way. i know sprint handles floats no problem b/c i''m using it all over the place to print out floats. wv an vsprintf i''ve never used before so don''t know about those.
va_arg( argv, float) is returning bad data. sp maybe that doesn''t handle floats. which doesn''t make any sense. arg
-me
va_arg( argv, float) is returning bad data. sp maybe that doesn''t handle floats. which doesn''t make any sense. arg
-me
ooh, thanls for making me think about supported data types. changine it to:
va_arg( argv, double );
makes it all good.
nice. now i can get back to real work
-me
va_arg( argv, double );
makes it all good.
nice. now i can get back to real work
-me
LessBread''s solution is the best one mentioned here, as it supports all common data-formats (incl. int''s, char''s, strings, etc.). You should go for that solution.
-Neophyte
-Neophyte
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement