Author Topic: Verify the number from user's input  (Read 4662 times)

0 Members and 1 Guest are viewing this topic.

MeasureUp

  • Bull Frog
  • Posts: 461
Verify the number from user's input
« on: March 20, 2014, 09:28:17 PM »
How to verify a user's input is an integer or a real number?
Then how to check the number of decimal digits if the user's input is a real number?
Thanks for your help.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Verify the number from user's input
« Reply #1 on: March 20, 2014, 10:52:45 PM »
Sometimes personal Library functions are handy :)

If you're concerned about input ask for the type you want.
Code - Auto/Visual Lisp: [Select]
  1. (setq i1 (getint "Specify Integer number value"))
  2. (setq r1 (getreal "Specify Real number value"))

If you have the date data , check if it's a number

Code - Auto/Visual Lisp: [Select]
  1. (setq integer    4)
  2. (setq real       5.1)
  3. (setq edgecase   6.0)
  4. (setq real-4Places1 4.1111)

Code - Auto/Visual Lisp: [Select]
  1. (and (numberp integer)
  2.      (numberp real)
  3.      (numberp edgecase)
  4.      (numberp real-4Places1)
  5. )
  6.  

If it's a number, check it's type
Code - Auto/Visual Lisp: [Select]
  1. (defun kdub:real-p (arg)  (equal (type arg) 'real))
  2. (defun kdub:integer-p (arg)  (equal (type arg) 'int))

If it's a real, check the decimal places.
Code - Auto/Visual Lisp: [Select]
  1. (defun kdub:decimalPlaces (nval)
  2.   (if (zerop (rem nval 1))
  3.     0
  4.     (- (strlen (vl-prin1-to-string (rem nval 1))) 2)
  5.   )
  6. )

Code - Auto/Visual Lisp: [Select]
  1. (kdub:decimalPlaces integer)       ;=> 0
  2.  
  3. (kdub:decimalPlaces real)          ;=> 1
  4. (kdub:decimalPlaces real-4Places1) ;=> 4
  5. (kdub:decimalPlaces edgecase)      ;=> 0
  6.  
  7.  
« Last Edit: March 21, 2014, 09:11:44 AM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

MeasureUp

  • Bull Frog
  • Posts: 461
Re: Verify the number from user's input
« Reply #2 on: March 21, 2014, 12:27:24 AM »
Sorry, I should give more details. Here is the portion of my code:
Quote
(getreal "\nEnter the number: ")
This means that the user can enter an integer or a real nember.
I don't want to give the user to choose the type of input by using both "getreal" and "getini" in the code.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Verify the number from user's input
« Reply #3 on: March 21, 2014, 01:53:18 AM »
You just need to think sideways for a second.

Code - Auto/Visual Lisp: [Select]
  1. ;;an absurdity: kdub@theSwamp
  2.  
  3. (defun _getreal (promptmsg / returnvalue)
  4.         (setq returnvalue (vl-catch-all-apply 'getreal (list promptmsg)))
  5.       )
  6.     ;; ESC was pressed.
  7.     (setq returnvalue nil)
  8.   )
  9.   (if returnvalue
  10.     (float returnvalue)
  11.     nil
  12.   )
  13. )
  14.  
  15.  
  16. (_getreal "Specify Real number value")
  17.  
  18.  


//-----------------------------------
Perhaps I should explain that this code is a JOKE.

MeasureUp,
You have obviously done no testing with the code I posted.

Please explain how
Code - Auto/Visual Lisp: [Select]
  1. (setq nVar (getreal "\nEnter the number: "))
can accept anything other than a real number.
 
What is the value of nVar if you respond to the prompt with the integer 42 ??
« Last Edit: March 21, 2014, 02:36:50 AM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: Verify the number from user's input
« Reply #4 on: March 21, 2014, 04:19:35 AM »
an example:
Code: [Select]
; BitVal values:
; 0 - only number
; 1 - reject positive (and zero)
; 2 - reject zero
; 4 - reject negative
; 8 - reject real, must be integer
;
; value 3 is equal to 1
; value 11 is equal to 9
; values 5, 7, 13, 15 not have sense > rejects all values
; ex: (ALE_CheckNumber -1 6 nil nil)
;
(defun ALE_CheckNumber (ImpVal BitVal FrmVal ToVal / ErrStr)
  (cond
    ( (or
        (not (numberp ImpVal))                                 ; not number
        (and (= 1 (logand 1 BitVal)) (= (abs ImpVal)  ImpVal)) ; is positive
        (and (= 2 (logand 2 BitVal)) (zerop ImpVal))           ; is zero
        (and (= 4 (logand 4 BitVal)) (/= (abs ImpVal) ImpVal)) ; is negative
        (and (= 8 (logand 8 BitVal)) (= 'REAL  (type ImpVal))) ; is real, not int
        (and FrmVal ToVal      (not (<= FrmVal ImpVal ToVal))) ; not in range
      )
      (cond
        ( (= BitVal  1) (setq ErrStr "The number must be negative") )
        ( (= BitVal  2) (setq ErrStr "The number must be different from zero") )
        ( (= BitVal  4) (setq ErrStr "The number must be positive or zero") )
        ( (= BitVal  8 ) (setq ErrStr "The number must be integer") )
        ( (= BitVal  6) (setq ErrStr "The number must be positive and different from zero") )
        ( (= BitVal  9) (setq ErrStr "The number must be integer negative") )
        ( (= BitVal 10) (setq ErrStr "The number must be integer and different from zero") )
        ( (= BitVal 12) (setq ErrStr "The number must be integer positive or zero") )
        ( (= BitVal 14) (setq ErrStr "The number must be integer positive and different from zero") )
        ( T             (setq ErrStr "Value must be a number") )
      )
      (if (and FrmVal ToVal)
        (setq ErrStr (strcat ErrStr " and inclusive among " (ALE_RtoS_DZ8 FrmVal) " and " (ALE_RtoS_DZ8 ToVal) "!"))
        (setq ErrStr (strcat ErrStr "!"))
      )
    )
    ( ImpVal )
  )
)

(defun ALE_RtoS_DZ8 (ReaVal / CurDZn OutVal)
  (if (= 8 (setq CurDZn (getvar "DIMZIN")))
    (setq CurDZn nil)
    (setvar "DIMZIN" 8 )
  )
  (setq OutVal (rtos ReaVal 2))
  (and CurDZn (setvar "DIMZIN" CurDZn))
  OutVal
)

NICK_VNV

  • Newt
  • Posts: 63
Re: Verify the number from user's input
« Reply #5 on: March 21, 2014, 05:06:45 AM »
Sorry, I should give more details. Here is the portion of my code:
Quote
(getreal "\nEnter the number: ")
This means that the user can enter an integer or a real nember.
I don't want to give the user to choose the type of input by using both "getreal" and "getini" in the code.

Check if my code can help you
Code: [Select]
(setq a (getreal "\nEnter the number: "))

(if (> (rem a 1) 0)
  (setq typ "REAL")
  (setq typ "INT")
)
(princ (strcat "\nEntered type is " typ))
(princ)

And then
If it's a real, check the decimal places.
Code - Auto/Visual Lisp: [Select]
  1. (defun kdub:decimalPlaces (nval)
  2.   (if (zerop (rem nval 1))
  3.     0
  4.     (- (strlen (vl-prin1-to-string (rem nval 1))) 2)
  5.   )
  6. )
(kdub:decimalPlaces a)
« Last Edit: March 21, 2014, 05:18:16 AM by NICK_VNV »
Sorry for my English...

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Verify the number from user's input
« Reply #6 on: March 21, 2014, 05:24:00 AM »
Sorry, I should give more details. Here is the portion of my code:
Quote
(getreal "\nEnter the number: ")
This means that the user can enter an integer or a real number.
I don't want to give the user to choose the type of input by using both "getreal" and "getint" in the code.

MeasureUp,
You may need to restate your requirements.

What input do you want from the user ?
Do you want to use getReal() for decimal numbers ?
Do you want to use getInt() for integer numbers ?
or do you want to do something else like getAnyNumber() for a particular reason ?
If that is the case do you need to know the type? and why ? 

A little more information at the start would make resolving your problem possible.


« Last Edit: March 21, 2014, 05:32:01 AM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Verify the number from user's input
« Reply #7 on: March 21, 2014, 07:30:38 AM »
Shot in the dark-
The Op wants to interpret user input from getstring as a number (my idea)
If no decimal is used the input is integer.
If decimal then how many places entered.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

snownut2

  • Swamp Rat
  • Posts: 971
  • Bricscad 22 Ultimate
Re: Verify the number from user's input
« Reply #8 on: March 21, 2014, 07:46:50 AM »
CAB,

According to OP's second post he is using a "Getreal" function to retrieve user input, apparently he does not believe its a real number if user enters non-decimal input.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Verify the number from user's input
« Reply #9 on: March 21, 2014, 07:56:36 AM »
I'm just saying that if you want the number of trailing zeros you need to use getstring and convert to a number.
And use this on the string.
Code: [Select]
  ;; get precision of a decimal number string including trailing zeros
  (defun getPrec (str)
    (if (vl-string-search "." str)
      (- (strlen str)(vl-string-search "." str) 1)
      0
    )
  )
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Verify the number from user's input
« Reply #10 on: March 21, 2014, 07:57:54 AM »
You never know exactly what the OP wants until you get "That's it" from the OP. 8)
It's quite a dart game answering some of these questions.
 
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Verify the number from user's input
« Reply #11 on: March 21, 2014, 08:50:39 AM »
You never know exactly what the OP wants until you get "That's it" from the OP. 8)
It's quite a dart game answering some of these questions.

I'm not quite so generous Alan.

Solving the problem is usually time consuming ... solving the question is a bloody waste of good time.

and my football team lost tonight so I'm not happy anyway .. but at least they tried.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: Verify the number from user's input
« Reply #12 on: March 21, 2014, 08:53:43 AM »
You never know exactly what the OP wants until you get "That's it" from the OP. 8)
It's quite a dart game answering some of these questions.
maybe this:
Code: [Select]
; ---------- Source files from: --------------------------------------------
; These functions are freeware courtesy of the author's of "Inside AutoLisp"
; for rel. 10 published by New Riders Publications. This credit must
; accompany all copies of this function.
; Modified by Alessi Marc'Antonio
;
; UINT   Funzione di interfaccia utente per numeri interi.
; IGtBit > (0 for none) and KwdStr key word ("" for none) same of INITGET
; PrmStr = prompt string, a default integer will be added
;          as <DefInt> (nil for none) and a : will be added
;
; Example:
; (setq #GlVal (uInt 7 "" "How many times" #GlVal))
;
(defun uInt (IGtBit KwdStr PrmStr DefInt / InpVal)
  (if DefInt
    (setq PrmStr (strcat "\n" PrmStr " <" (itoa DefInt) ">: ")
          IGtBit (logand IGtBit 254)
    )
    (setq PrmStr (strcat "\n" PrmStr ": "))
  )
  (initget IGtBit KwdStr)
  (setq InpVal (getint PrmStr))
  (if InpVal InpVal DefInt)
)
;

; UREAL  Funzione di interfaccia utente per numeri reali.
; IGtBit > (0 for none) and KwdStr key word ("" for none) same of INITGET
; PrmStr = prompt string, a default real will be added
;          as <DefRea> (nil for none), a : will be added.
;
; Example:
; (setq #GlVal (uReal 1 "" "Scale factor" #GlVal))
;
(defun uReal (IGtBit KwdStr PrmStr DefRea / InpVal)
  (if DefRea
    (setq
      PrmStr (strcat "\n" PrmStr " <" (ALE_RTOS_DZ8 DefRea) ">: ")
      IGtBit (logand IGtBit 254)
    )
    (setq PrmStr (strcat "\n" PrmStr ": "))
  )
  (initget IGtBit KwdStr)
  (setq InpVal (getreal PrmStr))
  (if InpVal InpVal DefRea)
)
;ALE_RTOS_DZ8 see previous post

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Verify the number from user's input
« Reply #13 on: March 21, 2014, 09:02:23 AM »

Code: [Select]
; ---------- Source files from: --------------------------------------------
; These functions are freeware courtesy of the author's of "Inside AutoLisp"
; for rel. 10 published by New Riders Publications. This credit must
; accompany all copies of this function.
< .. >

That's a familiar blast from the past. :)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: Verify the number from user's input
« Reply #14 on: March 21, 2014, 09:12:43 AM »

Code: [Select]
; ---------- Source files from: --------------------------------------------
; These functions are freeware courtesy of the author's of "Inside AutoLisp"
; for rel. 10 published by New Riders Publications. This credit must
; accompany all copies of this function.
< .. >

That's a familiar blast from the past. :)
   ...if I remember correctly it was around 1990... I continue to use it again!


Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: Verify the number from user's input
« Reply #15 on: March 21, 2014, 04:06:41 PM »
Sorry, I should give more details. Here is the portion of my code:
Quote
(getreal "\nEnter the number: ")
This means that the user can enter an integer or a real nember.
I don't want to give the user to choose the type of input by using both "getreal" and "getini" in the code.

I believe the confusion in this thread arises because ℤ ⊂ ℝ, that is the set of integers is contained in the set of real numbers - hence by prompting the user for a real number, they are also permitted to enter an integer.

I'm also unsure what you are trying to achieve, but here is my offering for darts game:

Code - Auto/Visual Lisp: [Select]
  1. (defun getreal-or-int ( msg / dim num )
  2.     (if (setq num (getreal msg))
  3.         (progn
  4.             (setq dim (getvar 'dimzin))
  5.             (setvar 'dimzin 8)
  6.             (setq num (read (rtos num 2 15)))
  7.             (setvar 'dimzin dim)
  8.         )
  9.     )
  10.     num
  11. )

Code - Auto/Visual Lisp: [Select]
  1. Command: (getreal-or-int "\nEnter a number: ")
  2. Enter a number: 2
  3. 2
  4.  
  5. Command: (getreal-or-int "\nEnter a number: ")
  6. Enter a number: 2.0
  7. 2
  8.  
  9. Command: (getreal-or-int "\nEnter a number: ")
  10. Enter a number: 2.5
  11. 2.5

Hopefully I'll hit the treble-20  8-)

GP

  • Newt
  • Posts: 83
  • Vercelli, Italy
Re: Verify the number from user's input
« Reply #16 on: March 22, 2014, 10:26:08 AM »
darts game 2.0   :-)

Code: [Select]
(if (zerop (- num (fix num))) (atoi (rtos num)) num)

2    ->  2
2.0  ->  2
2.5  ->  2.5


Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: Verify the number from user's input
« Reply #17 on: March 22, 2014, 10:31:18 AM »
@GP, you might need to use (atoi (rtos num 2))  ;-)

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Verify the number from user's input
« Reply #18 on: March 22, 2014, 02:33:15 PM »
Hi,

No need to convert to string and convert back:
Code - Auto/Visual Lisp: [Select]
  1. (defun getRealOrInt (msg / num int)
  2.   (if (setq num (getreal msg))
  3.     (if (= (setq int (fix num)) num)
  4.       int
  5.       num
  6.     )
  7.   )
  8. )
Speaking English as a French Frog

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Verify the number from user's input
« Reply #19 on: March 22, 2014, 02:50:14 PM »
Hi,

No need to convert to string and convert back:
Code - Auto/Visual Lisp: [Select]
  1. (defun getRealOrInt (msg / num int)
  2.   (if (setq num (getreal msg))
  3.     (if (= (setq int (fix num)) num)
  4.       int
  5.       num
  6.     )
  7.   )
  8. )

Very elegant gile.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Verify the number from user's input
« Reply #20 on: March 22, 2014, 04:14:22 PM »
Thanks Kerry, but it wasn't so difficult  :wink:
Speaking English as a French Frog

GP

  • Newt
  • Posts: 83
  • Vercelli, Italy
Re: Verify the number from user's input
« Reply #21 on: March 23, 2014, 06:54:41 AM »
@GP, you might need to use (atoi (rtos num 2))  ;-)

No need to convert to string and convert back:

True...
thanks.  :-)

MeasureUp

  • Bull Frog
  • Posts: 461
Re: Verify the number from user's input
« Reply #22 on: March 23, 2014, 07:17:51 PM »
WOW! It's amazing. I just come back from a buzy time.
Thanks to everyone.

This is my proposal:
Quote
(setq UserInput (getreal "\nEnter the number: "))

(if (UserInput is an integer)
    (Option 1 ...)
   (progn (Get_Number_of_Decimal) ;;; if the input is a real"
         (Option 2 ...)
   )
)

Kerry, hope your football term wins next time.
NICK_VNV & CAD, your codes are simple and straightforward.
Quote
(setq a (getreal "\nEnter the number: "))
(if (> (rem a 1) 0)
  (setq typ "REAL")
  (setq typ "INT")
)
(princ (strcat "\nEntered type is " typ))
(princ)
Quote
;; get precision of a decimal number string including trailing zeros
  (defun getPrec (str)
    (if (vl-string-search "." str)
      (- (strlen str)(vl-string-search "." str) 1)
      0
    )
  )

gile, your are so smart!
Lee, you always think something different from others.

You did not waste your time. I can learn from your ideas.
Thanks to swamp people.  :-) :-) :-)
« Last Edit: March 24, 2014, 01:06:50 AM by MeasureUp »

Peter2

  • Swamp Rat
  • Posts: 650
Re: Verify the number from user's input
« Reply #23 on: July 27, 2014, 10:55:53 AM »
Hi,

No need to convert to string and convert back:
Code - Auto/Visual Lisp: [Select]
  1. (defun getRealOrInt (msg / num int)
  2.   (if (setq num (getreal msg))
  3.     (if (= (setq int (fix num)) num)
  4.       int
  5.       num
  6.     )
  7.   )
  8. )

Hi

maybe I missed something, but using this code I get:
Code - Auto/Visual Lisp: [Select]
  1. Befehl: (getrealorint "2.0")
  2. 2
  3. nil

It seems that "getreal" needs a "real user input" and not only a value??
Peter

AutoCAD Map 3D 2023 German (so some technical terms will be badly retranslated to English)
BricsCAD V23

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Verify the number from user's input
« Reply #24 on: July 27, 2014, 11:32:52 AM »
Here using 2006ACAD I get 2
It will accept 2 or 2.0
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Peter2

  • Swamp Rat
  • Posts: 650
Re: Verify the number from user's input
« Reply #25 on: July 27, 2014, 11:52:25 AM »
Here using 2006ACAD I get 2
It will accept 2 or 2.0
You mean "2" or "2.0", right?
Peter

AutoCAD Map 3D 2023 German (so some technical terms will be badly retranslated to English)
BricsCAD V23

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: Verify the number from user's input
« Reply #26 on: July 27, 2014, 12:01:16 PM »
Here using 2006ACAD I get 2
It will accept 2 or 2.0
You mean "2" or "2.0", right?

Note that the argument passed to gile's function is the prompt string to be displayed to the user for the getreal function, not the value to be interpreted.

Peter2

  • Swamp Rat
  • Posts: 650
Re: Verify the number from user's input
« Reply #27 on: July 29, 2014, 04:25:18 AM »
Note that the argument passed to gile's function is the prompt string to be displayed to the user for the getreal function, not the value to be interpreted.
Ooh - I see. Thank you.
Peter

AutoCAD Map 3D 2023 German (so some technical terms will be badly retranslated to English)
BricsCAD V23