Printing float number in MessageBox with Unicode

This is a discussion on Printing float number in MessageBox with Unicode within the Windows Programming forums, part of the Platform Specific Boards category; I am trying to print a float number in a MessageBox while Unicode is defined. I found with Google that ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    44

    Printing float number in MessageBox with Unicode

    I am trying to print a float number in a MessageBox while Unicode is defined. I found with Google that it is possible to print a float number by using sprintf to convert a float into a string which can be printed with MessageBox. But this is without Unicode. With Unicode I would expect that wsprintf would do the same thing, but I stil get strange characters in my MessageBox. Does anybody know how to do this?
    Thanx.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    Code:
    wchar_t msg[20];
    float my_float = 10.0f;
    wsprintf(msg, L"%f", my_float);
    MessageBox(msg, ...);
    Should work, assuming I haven't messed up the arguments.
    If it doesn't work, then I suggest you post the smallest possible compilable example that shows the problem.
    Last edited by Elysia; 02-16-2010 at 02:44 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    That "%f" should be L"%f".
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    As you can not determine the precision (length) of the float, I always use;

    _snwprintf() / _snprintf()

    to avoid buffer-overruns.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    44
    thanks for the replies.
    Elysia, that's exactly how I figured it should work, but when I do it, the MessageBox prints 'f'.
    I think it has to do with some strange settings in Visual Studio 2008, because when I do the same thing in a 'New Project' it does print the float number.
    The strange thing is that in both cases I have started with a Windows Application as 'a new Project' and haven't touched any Project Settings since. I guess Visual Studio must have been changing settings in the mean time, but I have no clue what.

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    44
    hmmm, I found that if I use 'Debug' mode in Visual Studio and subsequently use 'Start without Debugging', the float number will be printed. But of course I have no executable after that
    I have been using the 'Release' all the time and then I get a 0.0000 where the float should be. Anybody an idea what is going on there?

  7. #7
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    611
    What's your code look like? wsprintf doesn't support floating types (see the lack of f, e, and g in the table), so I'm not sure how you're getting anything other than "f" at all.

    I mean, ret = 1 and buffer of "f" is all I get from the below code on 7 Ultimate:
    Code:
    #define WIN32_LEAN_AND_MEAN
    #include <windows.h>
    #include <iostream>
    
    int main()
    {
        char buffer[10];
        int ret = wsprintfA(buffer, "%f", 89.99);
        std::cout << "wsprintfA returned " << ret << " and buffer of " << buffer << '\n';
        WCHAR wBuffer[10];
        ret = wsprintfW(wBuffer, L"%f", 89.97);
        std::wcout << L"wsprintfW returned " << ret << " and buffer of " << wBuffer << '\n';
    }

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    44
    Hi adeyblue, you're right, with wsprintf I get only an 'f' printed.
    I found a function MessageBoxPrintf() in the book "Programming Windows" by Charles Petzold, which can print a float. I should have said that.
    The only problem I have now is that in Release mode in Visual studio 0.0000 is printed and in Debug mode the correct float is printed, which is useless of course, since I need an executable.
    Debugging goes fine untill I close the application, because then I get a message: "Windows has triggered a breakpoint in Annualoutput.exe. This may be due to a corruption of the heap, which indicates a bug in Annualoutput.exe or any of the DLLs it has loaded."
    So I guess this means I don't free memory in the correct way?

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    Since you're using C++, may I suggest you use boost? It has a function called boost::lexical_cast that can convert any type into any other, provided it's supported, of course.
    It should be able to convert a float to std::wstring, I believe (haven't tested).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Feb 2010
    Posts
    44
    I actually use C, but in Visual Studio I think it is possible to mix some C and C++ code.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    std::cout is C++. But C++ allows you to freely mix C and C++ code, within some limitations. Of course, such should be avoided preferably.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Promblem with code
    By watchdogger in forum C Programming
    Replies: 18
    Last Post: 01-31-2009, 05:36 PM
  2. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  3. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  4. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM
  5. Newton-Raphson number squaring method/pointers
    By inakappeh in forum C Programming
    Replies: 2
    Last Post: 08-29-2001, 11:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21