Thread: double in a MessageBox

  1. #1
    Registered User
    Join Date
    Sep 2001
    Location
    England
    Posts
    121

    double in a MessageBox

    Very simple ( I hope ) Windows programming question here, I have a string value taken from an edit box, which I have converted into a double with atof(), I then want to do some calculations and open a MessageBox with the answer in it in the form:

    MessageBox(Answer, "Solution:", MB_OK);

    However, the first parameter, 'Answer' can only be a string value (I think), so how do I:

    a) convert Answer from a double to a string so I can use it in MessageBox()

    -or-

    b) show a double value in place of Answer

    I have looked for a function to convert double to string but I havent found any, only functions like atof() or strtod() for the other way around, but when I try to use Answer in MessageBox() as a double I get:

    error C2664: 'MessageBoxA' : cannot convert parameter 1 from 'double' to 'const char *'
    There is no context in which this conversion is possible

    Thanks for any help

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    8

    Lightbulb

    you can use this:
    char *_ecvt( double value, int count, int *dec, int *sign );

    example;

    int decimal, sign;
    int precision;
    if (mpg < 10000) precision = 5;
    if (mpg < 1000) precision = 4;
    if (mpg < 100) precision = 3;
    if (mpg < 10) precision = 2;

    char* buffer;
    buffer = ecvt(mpg, precision, &decimal, &sign );
    for (int x = 0;x < precision;x++){
    if (x == decimal)
    m_gasMileage_Entry[m_Fcount].miles_per_gallon += '.';
    m_gasMileage_Entry[m_Fcount].miles_per_gallon +=
    buffer[x];
    }

  3. #3
    Registered User
    Join Date
    Sep 2001
    Location
    England
    Posts
    121

    one last thing

    I've implemented teh ecvt() function and it works like it should, however, when the input (in your example's case mpg) is a decimal it rounds the number to the nearest int, I assume because the second parameter or esvt only takes an integer, how do I get round this to let my program understand decimals?

    P.S I changed some of code, digital dropout, because the output of ecvt to buffer was always 10x as big has it should have been ?? So I changed all of your precision values to 1 less eg:

    if (mpg < 10000) precision = 4;
    if (mpg < 1000) precision = 3;
    if (mpg < 100) precision = 2;
    if (mpg < 10) precision = 1;

    seems to work , apart from the decimal thing

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Another option is to use sprintf() in <stdio.h>.

    assuming:
    double Answer;
    char str[31];

    sprintf(str,"The solution is %lf",Answer);
    MessageBox(NULL, str, "Solution:", MB_OK);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  2. Conversion From C++ To C
    By dicon in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 02:54 PM
  3. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 09:13 AM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 06:54 PM