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

0 Members and 1 Guest are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12914
  • 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: 12914
  • 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: 462
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: 653
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: 653
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: 12914
  • 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: 653
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