Link to home
Start Free TrialLog in
Avatar of AmarjitSingh
AmarjitSingh

asked on

Storing Double

I'm currently constructing an MFC application.

I desire for a double, which a user types into an Edit Control to be stored in a buffer, which is achieved using the following code segment:

CString cBuffer[5];
GetDlgItem(EDIT1)->GetWindowText(cBuffer[0]);

* EDIT1 is the member variable of an the edit control containing the double.

I'm confident in the functionality of this code, but I want the double stored in the buffer to be multiplied by 900, which I then want to be displayed in another edit control (EDIT2):

EDIT2 = atof(cBuffer[0]) * 900; // Convert the buffer into double and multiply

I don't know if that is correct, but I'm informed proceeding this code, the following must be present:

UpdateData(FALSE);

Whether it's intended to be TRUE or FALSE is beyond me ...

Many thanks for helping - I had the code functioning before, but I deleted it, 'cos I was no longer working on the project :'(
Avatar of stefan73
stefan73
Flag of Germany image

Hi AmarjitSingh,
EDIT2 = atof(cBuffer[0]) * 900; // Convert the buffer into double and multiply

Try SetWindowText instead.

Cheers,
Stefan
Avatar of AlexFM
AlexFM

UpdateData(TRUE) - copy values from dialog controls to variables

UpdateData(FALSE) - copy values from variables to dialog controls.

If EDIT2 is CString variable linked to edit control, you need UpdateData(FALSE);

If you use CEdit class members, you dont' need UpdateData, use Set/GetWindowText.
Avatar of AmarjitSingh

ASKER

I have informed myself of the usage of the Set/GetWindowText function, courtesy of MSDN.

However, AlexFM, it is mandatatory for me utilise UpdateData, because my Edit Control's member variables are Values instead of Controls.

On that point still, should the Member Variables be Control Variables or not?
This code is a little confusing.

If you want doubles, why not make the variable type on the control be double instead of CString?

Assume m_double1 and m_double2 are variables attached to CEdit boxes with resource ids EDIT1 and EDIT2:

Then do this:

UpdateData(); // loads m_double1 and m_double2
m_double2 = m_double1 * 900;
UpdateData(FALSE); // puts them back on the dialog


If you really want to use strings - assume EDIT1 and EDIT2 are the variables tied to the edit boxes:

UpdateData(); // EDIT1 now contains the string from the edit control
EDIT2.Format("%f", atof((LPCSTR)EDIT1*900);
UpdateData(FALSE); // puts the string back in the edit boxes


This line:

> GetDlgItem(EDIT1)->GetWindowText(cBuffer[0]);

is trying to use EDIT1 as a resource id, if it is a variable tied to a control this won't work

> EDIT2 = atof(cBuffer[0]) * 900; // Convert the buffer into double and multiply

At this point I don't know what EDIT1 and EDIT2 are. This line only works if EDIT2 is a float.

I need to find a way of converting a CString variable, which is holding EDIT1 to a double using atof. Every time, the compiler springs up with this:

cannot convert parameter 1 from 'double' to 'LPCTSTR'
        There is no context in which this conversion is possible

So if converting CString to double is outside the realms of possibility, what alternative to using CString is present?
ASKER CERTIFIED SOLUTION
Avatar of wayside
wayside

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of leflon
Hi AmarjitSingh,

> cannot convert parameter 1 from 'double' to 'LPCTSTR'
>        There is no context in which this conversion is possible

if you try somethingthe error message makes sence if you try something like
double d;
d = atof(EDIT1);

and EDIT1 is a member variable of type double (which it seems to be from your description so far). in this case you don't need any conversions, cause EDIT1 will contain the number entered in the editbox (if the message mapping is setup OK)

leflon
wayside, your double to cstring function was what I required and it worked fantastically!

What does the "%f" parameter do? As I have seen other variations such as "%.2f"
"%f" means "print this argument as a floating point number"
"%.2f" means "print this argument as a floating point number and print exactly 2 digits after the decimal"

There are tons of formats, with lots of modifiers.

For example, "%d" is for an integer, "%6d" says make the integer take up at least 6 characters, "%06d" says make the int take up at least 6 characters and if it is less pad with zeros.

Check these links for a full description of all of them:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_format_specification_fields_.2d_.printf_and_wprintf_functions.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_printf_type_field_characters.asp
Thanks man - you rightfully deserve these points.