Solved

Storing Double

Posted on 2004-03-30
10
4,958 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 125 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

733 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