Solved

Storing Double

Posted on 2004-03-30
10
4,951 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
10 Comments
 
LVL 12

Expert Comment

by:stefan73
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 14

Expert Comment

by:wayside
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 14

Accepted Solution

by:
wayside earned 125 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
"%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
Comment Utility
Thanks man - you rightfully deserve these points.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now