Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Storing Double

Posted on 2004-03-30
10
Medium Priority
?
4,969 Views
Last Modified: 2010-08-05
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
Comment
Question by:AmarjitSingh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 12

Expert Comment

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

Try SetWindowText instead.

Cheers,
Stefan
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 10714567
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
 

Author Comment

by:AmarjitSingh
ID: 10714711
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.

 
LVL 14

Expert Comment

by:wayside
ID: 10715078
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
 

Author Comment

by:AmarjitSingh
ID: 10715605
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
 
LVL 14

Accepted Solution

by:
wayside earned 500 total points
ID: 10715928
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
 
LVL 7

Expert Comment

by:leflon
ID: 10716255
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
 

Author Comment

by:AmarjitSingh
ID: 10724924
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
 
LVL 14

Expert Comment

by:wayside
ID: 10725165
"%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
 

Author Comment

by:AmarjitSingh
ID: 10725212
Thanks man - you rightfully deserve these points.
0

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question