-
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
-
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];
}
-
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 :(
-
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);