Archived

This topic is now archived and is closed to further replies.

Palidine

variable arguments driving me insane

Recommended Posts

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 );
    
}
  

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites