• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4972
  • Last Modified:

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 :'(
0
AmarjitSingh
Asked:
AmarjitSingh
1 Solution
 
stefan73Commented:
Hi AmarjitSingh,
EDIT2 = atof(cBuffer[0]) * 900; // Convert the buffer into double and multiply

Try SetWindowText instead.

Cheers,
Stefan
0
 
AlexFMCommented:
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.
0
 
AmarjitSinghAuthor Commented:
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?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
waysideCommented:
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.

0
 
AmarjitSinghAuthor Commented:
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?
0
 
waysideCommented:
Converting a CString to a double is easy:

CString mycstring = "3.0";
double mydouble = atof((LPCSTR)mycstring);

To convert a double back to a CString, the easiest way is:

CString mycstring2;
double mydouble2  = 3.0;
mycstring2.Format("%f", mydouble2);

If this doesn't help get you where you need to be, please post your entire function, the variable declarations in the include file, and the DoDataExchange function. This will allow me to see exactly what you are trying to do and where the problem is.
0
 
leflonCommented:
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
0
 
AmarjitSinghAuthor Commented:
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"
0
 
waysideCommented:
"%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
0
 
AmarjitSinghAuthor Commented:
Thanks man - you rightfully deserve these points.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now