Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check if a CString is a double 1

Status
Not open for further replies.

Lammbock

Programmer
Jun 3, 2003
32
0
0
DE
Hi.

Im new to VC++ and have some problem creating a function which is given a CString and is supposed to return 1 if the CString is a valid positive double.
I think of something like

int CheckForDouble (CString input)
{
if ( input is a positive double )
{
return 1;
}
return 0;
}

If its a double I can safely use atof() in my program to work with the converted CString

if ( CheckForDouble ( stringvar ) == 1)
{
doublevar = atof ( stringvar )
}

Thanks for helping me get along!
 
No, there is no standard library way to do this.

My algorithm would check the string for a pattern: (if you are familiar with regexps this will make sense, but otherwise you'll probably be best not to try to understand it.)

Code:
^[+-]?[0-9]+(\.[0-9]+)?([eE][+-]?[0-9]+)?$

To use this, you need a regular expression library... try searching google for "C regular expression library".

[sub]I REALLY hope that helps.[/sub]
Will
 
First go throught the string and make sure that you have only characters that would belong in a double (0,1,2,3,4,5,6,7,8,9,.,e,+,-). If you find something that does not belong return indicating not a double.

Second, try using the return of atof for your indication if you have a valid double. It will return a double equal to the value of the number or 0.0 if it fails. If it returns a value other than 0.0 then your function should return indicating that the string is a double.

Third, see if your string equals 0.0. Search for the period. Remove any additional 0 ("00000.00000" = "0.0"). Then see if it equal "0.0". If it does then it is again a valid double and your function should return indicating this result.

Hope this helps.

 
Hi,
the function that you can use is :
Code:
if(inputStr.FindOneOf(&quot;abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`~[]{}-_=+\\|'/?>,<&quot;) !=-1)
    {
    it's not double
    }
 
why not use a sscanf ?
this always returns 0 if it cannot convert the given value to what you told.

for example:
CString MyString = &quot;1.0&quot;;
double dummy;

if (sscanf(MyString.GetBuffer(1), &quot;%d&quot;, &dummy) == 0) {
return -1;
} else {
return 0;
}

or use sscanf directly



 
Hi,
try to use flex in unix or pclex in Windows for it and u can give the regular expression to match the double string.

If it matches u can convert it by atof().But u have to give some input file and some outfile bcos lex needs files.
Tell me whether it is possible to use lex or not.Lex creates the c source code so i think it wont be a problem for you and its fast.
^[+-]?[0-9]+(\.[0-9]+)?([eE][+-]?[0-9]+)?$

P B G Chandra Sekhar

 
Thank you all.
I used bcravens' idea and wrote a new function to scan the string.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top