Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function to validate string for numeric with decimal point
#51
the 2 extra ifs were for efficiency, not helping the user.

Anyways, none of those iFs are needed if the input is valid beforehand, you see.

Moneo, people have been using these types of things for a while now...
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Reply
#52
Quote:...Moneo, people have been using these types of things for a while now...
Well, if what you say is true, then I'm wrong. But I still feel that this unconventional data entry logic is dangerous for most experienced data entry clerks.
*****
Reply
#53
an example of this is in qbnews prog help... my reply to megaman......
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Reply
#54
Aga, couldn't find any section in qbnews called prog help.
*****
Reply
#55
http://forum.qbasicnews.com/viewtopic.php?t=3346
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Reply
#56
Here's my version of a program to do the numeric and decimal point validation using a function with only 4 if's.
Code:
DEFINT A-Z
DECLARE FUNCTION NumDecimal (Z$,DecimalPlaces)

cls
INPUT "Number of decimal places for this test (0-9) ";dp
if dp<0 or dp>9 then print "Invalid decimal places":system

do
  INPUT "Enter number to validate (X=Exit) ";Z$
  if ucase$(z$)="X" then system
  if NumDecimal(z$,dp) then print "Valid" else print "INVALID"
loop
end
REM *
REM *** CHECK FOR STRICTLY NUMERIC, NO NULL, ALLOW SPECIFIED DECIMAL PLACES.
REM *
FUNCTION NumDecimal (Z$,DecimalPlaces)
  NumDecimal=0                      'Initialize result to False.
  W$=Z$                             'Save original input because we may modify.
  IF W$="" THEN EXIT FUNCTION       'False if null.

  ZZP=INSTR(W$,".")                 'Scan for a decimal point.
  IF ZZP>0 and DecimalPlaces>0 THEN 'See if got one and decimalplaces not 0.  
     MID$(W$,ZZP,1)="0"             'Replace decpt with 0 to make numeric.
     IF ZZP < LEN(W$)-DecimalPlaces OR INSTR(W$,".") THEN EXIT FUNCTION 'chk dp
  END IF                                                      
  FOR ZZ = 1 TO LEN(W$)             'Now the rest should be numeric, check it.
      A=ASC(MID$(W$,ZZ,1))
      IF A<48 OR A>57 THEN EXIT FUNCTION
  NEXT ZZ

  NumDecimal = -1          'True

END FUNCTION
*****
Reply
#57
cheating. You missed one IF:

"9." gives VALID.

Plus, you have two ORs, which just hides the fact that they are really two IFs combined. Also, QB's ORs are inefficient since they scan everything first and then produce a result, instead of scanning the first, checking, then the second, and checking again.
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Reply
#58
AGA,

Within the function, which was the original challenge, there are only 4 if's, compound or not.

You will get a VALID for "9." or any number with a trailing decimal point, only when the number of decimal places is greater than zero. That is, if you specify 1 or more decimal places, then it is assumed that your result can handle up to that many decimals. For 2 decimal places, 123, 123., 123.0, or 123.00 are all valid.

Yes I agree that QB is inefficient in that it resolves every condition on a compound if. Don't most other language do the same?
*****
Reply
#59
Not C. I don't see what's so hard about making it efficient, anyways. Ah well.

Perhaps a re-processor for OR's and ANDs in IFs is a good idea...
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)