variable arguments driving me insane

Started by
4 comments, last by Palidine 21 years, 8 months ago
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:
  
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 );
    
}
  
Advertisement
Try it this way


      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]
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
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
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
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
cool. thanks for the late night help

implemented and working sweetly. got me though getting oct tree sorting working. woohoo!

-me

This topic is closed to new replies.

Advertisement