BoatProg

I am writing a model of a sailing boat using Visual C++.

Principal output from the model is a set of graphs of selected model variables against time. The required model variables are identified in a set of edit boxes, filled in by the user prior to running.

In the Code, how can I use the strings in the edit boxes to access the required model variables at run time



Re: Visual C++ Language Identifcation and access of variables from strings

_johnr_

Hi BoatProg,

You will need to give us some more information in order to answer this question. Are you using .NET, MFC, straight Win32 API calls or something else

John.





Re: Visual C++ Language Identifcation and access of variables from strings

Sdi

If your're working in raw Win32, you'll use GetDlgItemText() or (if you know that the input is digits) GetDlgItemInt(). If you're using MFC, the data-exchange mechanism handles the transfer. If you're using another framework, it probably has equivalent methods.



Re: Visual C++ Language Identifcation and access of variables from strings

BoatProg

Hi John,

Thanks for responding,

I am actually using Visual Studio 6 and have created the skeleton programme using the MFC Application Wizard. I guess the answer to your question is that I am using Win32 API calls embedded in MFC.

Hope this is the right information

The programme is up and running but currently uses a very clumsy method of selecting varaibles for plotting and would like to improve this.

Regards

David





Re: Visual C++ Language Identifcation and access of variables from strings

BoatProg

Hi Sdi

Thanks for the information which gets me part of the way there. The remaining difficulty, I think, is that having got hold of the strings from the edit boxes, how to use them, in the code, to access the model variables

Regards

David





Re: Visual C++ Language Identifcation and access of variables from strings

Nishant Sivakumar

You have the strings. You have the model variables. What's the difficulty here Just pass the strings to methods in the model class that expect them, and you should be on your way/




Re: Visual C++ Language Identifcation and access of variables from strings

Shakje

Assuming you want to change the strings into numbers, try atol or atoi or atof if you're using Win32 or MFC as I'm guessing you are. In .NET there's Parse methods inside the base types.




Re: Visual C++ Language Identifcation and access of variables from strings

Sdi

_ttol(). _stscanf(). You have the string representation of a value; from here it's basic C.



Re: Visual C++ Language Identifcation and access of variables from strings

BoatProg

Nishant

The strings contain the names of the model variables I wish to access. e.g. "xvelocity", "yvelocity", "Psi" etc

These names are entered by the user prior to running and change according to which variables he wishes to monitor.

how do I write the code to obtain the contents of the chosen variables e.g. xvelocity, yvelocity,Psi etc.

David





Re: Visual C++ Language Identifcation and access of variables from strings

Viorel.

BoatProg wrote:

Nishant

The strings contain the names of the model variables I wish to access. e.g. "xvelocity", "yvelocity", "Psi" etc

These names are entered by the user prior to running and change according to which variables he wishes to monitor.

how do I write the code to obtain the contents of the chosen variables e.g. xvelocity, yvelocity,Psi etc.

David

For selecting a variable by name, I think you can try either an "if" sequence, or a map. The next sample shows how three variables are registered in a map, and then one is accessed and changed:

Code Snippet

typedef CMap< CString, LPCTSTR, double *, double * > MAP;

double xvelocity = 1.0;

double yvelocity = 2.0;

double psi = 3.0;

MAP map;

map[_T("xvelocity")] = &xvelocity;

map[_T("yvelocity")] = &yvelocity;

map[_T("psi")] = &psi;

CString input;

input = _T("yvelocity");

double & selected_variable = *map[input];

selected_variable = 444.0; // now yvelocity is 444.0

I hope this makes sense.





Re: Visual C++ Language Identifcation and access of variables from strings

BoatProg

Thanks for that Viorel,

A sequence of "if" statements is what I currently use to do this but as the model may have 100 or so variables to choose from this is very clumsy.

I will investigate the use of "map".

Thanks again

David





Re: Visual C++ Language Identifcation and access of variables from strings

Sdi

It doesn't matter how many possible strings you might have to match; you still only need one 'if' statement to find the requested variable. What gets larger is the array that maps variable string names to the address (and maybe type) of the variable.



Re: Visual C++ Language Identifcation and access of variables from strings

BoatProg

Sorry Vorel, I dont propperly understand this.

what does _T("yvelocity") produce

Presumably this is some sort of integer as it is used as an index for the array named map.

If this is so, how big does array map have to be to allow for all possibilities of variable names

David





Re: Visual C++ Language Identifcation and access of variables from strings

Viorel.

BoatProg wrote:

Sorry Vorel, I dont propperly understand this.

what does _T("yvelocity") produce

Presumably this is some sort of integer as it is used as an index for the array named map.

If this is so, how big does array map have to be to allow for all possibilities of variable names

David

Visual Studio is able to compile your projects in ANSI or Unicode mode. In ANSI encoding, each character has a single 8-bit byte, whereas in Unicode it needs two bytes. Unicode is useful when you need more letters or symbols. There is a compiler option for this. The _T macro allows you to compile your source code in both modes without changes. Otherwise you will need to write "yvelocity" in ANSI mode, or L"yvelocity" in Unicode mode. The _T macro performs this adjustment automatically.





Re: Visual C++ Language Identifcation and access of variables from strings

BoatProg

Sorry Viorel, I still dont understand the method, I'll make a list of the things I dont understand:-

1. What sort of variable does the declaration "MAP map " produce

2. The expression _T("xvelocity") appears to be used as an index to array "map". What sort of quantity does _T("xvelocity") produce , integer , String etc

3. Variable "input" is declared as a string. How can a string be used as an index to array "map"

4. What range does variable "input" cover and so how big does the array "map" need to be

Thanks for your patience

David