Jump to content
aloy

Limiting the input value of a variable

Recommended Posts

aloy

How can I limit the input value for a variable?.

I tried the following lisp unsuccessfully:

 
(defun c:limit()
 (setq s 
(while (and (< s 2.5) (> s 7)) (setq s(getreal "\nGive value between 2.5 and 7:")))
)

Thanking in advance.

Aloy

Share this post


Link to post
Share on other sites
MSasu

Those conditions cannot be met simultaneously:

(defun c:limit()
(setq s 
(while ([color=red]or[/color] (< s 2.5)
           (> s 7.0))
 (setq s (getreal "\nGive value between 2.5 and 7.0: "))
)
)

Share this post


Link to post
Share on other sites
Tharwat
(defun c:limit (/ in)
 (while
   (not
     (or (< (setq in (getreal "\n Give value between 2.5 and 7:"))
        2.5
     )
     (> in 7)
     )
   )
    (princ "\n ** Must be Less than 2.5 or Biggar than 7.0 **")
 )
 (princ)
)

Share this post


Link to post
Share on other sites
MSasu

Tharwat, I'm afraid that your code is in fact rejecting the values it should accept and vice versa; there is also a conflict among prompter, respectively error message:

(defun c:limit (/ in)
 (while
   (not
     ([color=red]and[/color] ([color=red]>=[/color] (setq in (getreal "\n Give value between 2.5 and 7:"))
        2.5
     )
     ([color=red]<=[/color] in 7)
     )
   )
    (princ "\n ** Must be Less than 2.5 or Bigg[color=red]e[/color]r than 7.0 **")[color=red]   ;?!?[/color]
 )
 (princ)
)

Edited by MSasu
Fixed code to accept limits of interval too

Share this post


Link to post
Share on other sites
BlackBox

Also consider the INITGET function.

Share this post


Link to post
Share on other sites
Tharwat

My understanding of the Op goal is to accept the values between 2.5 to 7.0 and that's what my code does .

 

I don't get what conflict you are talking about !

 

I got not error message with Cad 2013

Share this post


Link to post
Share on other sites
MSasu
My understanding of the Op goal is to accept the values between 2.5 to 7.0 and that's what my code does .

That's true, "between"! Your routine accept only values outside this interval...

 

I got not error message with Cad 2013

Sorry, but mathematical logic isn't influenced by AutoCAD's version.

Share this post


Link to post
Share on other sites
aloy

Hi Msasu,

Thanks very much. It works fine.

Aloy

Share this post


Link to post
Share on other sites
aloy

Hi Tharwat,

Thanks for the code. It works; needs a correction to the string to give the intended meaning.

Regards,

Aloy

Share this post


Link to post
Share on other sites
aloy

RenderMan,

I thought 'INITGET' functions accept certain values, but not values within a defined range.

Regards,

Aloy

Share this post


Link to post
Share on other sites
BlackBox

Sorry for any confusion; perhaps I can clarify:

 

Technically, you're only presenting the user an illusion of limiting the value at the time of input, when in fact any real number input is accepted, and then checked to ensure the entered value falls within the desired range prior to continuing with your function.

 

INITGET is useful at 'Limiting the input value of a variable' (this thread's title), and not at ensuring that the user's real number input falls within a certain range after being entered, which is what is being done in the specific case you're after here.

 

It's use (INITGET) here would only be useful if you were attempting to prevent non-Nil, or even negative values being entered. Given the completeness of verifying that the user's input falls within the specified range, this is not necessary.

 

In any event, HTH

Share this post


Link to post
Share on other sites
aloy

RenderMan,

You are correct. Thanks,

Aloy

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×