Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28446 Discussions

Converting a character variable to a real variable

Carlos_S_
Beginner
2,696 Views

Please,

Someone could explain me how to convert a character variable get from an edit box to a real variable, using just a command? I am developing a Win32 program.

Thank you in advance.

Carlos Santos

0 Kudos
7 Replies
Arjen_Markus
Honored Contributor I
2,696 Views

I do not know how you get the character string from an edit box, but you can convert the character string to a real value by using an internal read:

character(len=20) :: string

real :: value

read( string, * )  value

(You will probably want to add some error handling code here)

0 Kudos
Carlos_S_
Beginner
2,696 Views

Dear arjenmarkus,

This kind of procedure doesn´t work in Win32 programs. I tried it but it didn´t work.

Thank you for your suggestion.

Carlos Santos.

 

0 Kudos
mecej4
Honored Contributor III
2,696 Views

You did not say what you tried, so there is not much to say about it. However, you seem to be unaware of a rather important principle. Fortran neither knows nor cares about an "edit box", just as it does not know about "black holes". A string is a string, whether it is engraved on a tombstone or is input through an edit box. A READ statement such as the one that Arjen Showed will work as long as the contents of string are suitable for being read as a real value. Whether the program is a Win32 program, a VMS program or one that controls a diesel engine is completely immaterial.

How you place the text that a user typed into an edit box into a string variable is specific to the OS and the API that you use. That, however, is not a Fortran question per se.

0 Kudos
andrew_4619
Honored Contributor II
2,696 Views

How is the edit box created?

0 Kudos
rase
New Contributor I
2,696 Views

Paul Burkardt has assembled a huge library of Fortran subroutines (http://people.sc.fsu.edu/~jburkardt/f_src/chrpak/chrpak.html) for converting characters and character strings to all kinds of variables and vice versa. Have a look at the descriptions and the sources. Internal read of a character string is widely used, also in Paul's routines, but he added a few error checks to analyse weird strings.

0 Kudos
andrew_4619
Honored Contributor II
2,696 Views
assuming your dialog edit box was made with the windows api and not quickwin:

use IFWIN, only: dword, SetDlgItemText, GetDlgItemText, handle
integer(handle)  :: hDlg
integer          :: ID_of_editbox_control
integer(DWORD)   :: ret
character(80)    :: gout, gin

! to set
gout='my text'//achar(0) ! null terminated string
ret = SetDlgItemText (hDlg, ID_of_editbox_control, gout ) !dialog handle and control ID

!to get text
ret = GetDlgItemText(hDlg, ID_of_editbox_control, gin,len(gin))

 

0 Kudos
jimdempseyatthecove
Honored Contributor III
2,696 Views

Adding to app4619's are the comments made earlier:

The data obtained in gin is the text of what was in the edit box. In the #7 example, this edit box is assumed to not have more than 80 characters of data. The data you type into the edit box must be convertible per-Fortran data representation:

Valid: 123456.78
Not valid: $123,456.78

Jim Dempsey

0 Kudos
Reply