Author Topic: If no input...  (Read 1971 times)

0 Members and 1 Guest are viewing this topic.

MeasureUp

  • Bull Frog
  • Posts: 463
If no input...
« on: February 01, 2011, 07:18:46 PM »
Is there any problem with the following?

Code: [Select]
(setq PipeOD 25.0)
(setq PipeID 20.0)
(setq TakeAction (getstring "Change Pipe ID? Yes or <No>: "))
; If the user presses the enter key without any other input, then do something:
(if (not TakeAction)
(progn (setq PipeID (getstring "Enter Pipe ID: "))
(setq PipeTHCIK (/ (- PipeOD PipeID) 2.0)))
(prompt (strcat "Pipe OD = " PipeOD "mm, " "Pipe ID = " PipeID "mm & " PipeTHICK "mm.")))

Thanks for your help.

Lee Mac

  • Seagull
  • Posts: 12916
  • London, England
Re: If no input...
« Reply #1 on: February 01, 2011, 07:19:46 PM »
Note that a null input to a getstring prompt returns an empty string ""

A few more things:

Code: [Select]
(setq[color=green] PipeOD 25.0[/color]) [color=green];; These are Reals, hence need to be converted to a string before used with 'strcat'[/color]
(setq [color=green]PipeID 20.0[/color])
(setq TakeAction (getstring "Change Pipe ID? Yes or <No>: "))

; If the user presses the enter key without any other input, then do something:
(if [color=blue](not TakeAction) ;; As above, this will never return T. Also, as you have used getstring, Yes or No will both return a non-nil value[/color]
  (progn
    [color=red](setq PipeID (getstring "Enter Pipe ID: ")) ;; Why are you prompting to Change the Pipe ID if the user has pressed enter for 'No'?[/color]
    (setq Pipe[color=red]THCIK[/color] (/ (- PipeOD PipeID) 2.0))
  )
  (prompt (strcat "Pipe OD = " [color=green]PipeOD[/color] "mm, " "Pipe ID = " [color=green]PipeID[/color] "mm & " Pipe[color=red]THICK[/color] "mm."))
[color=red]  ;; But the PipeTHICK variable will not exist at this point since it is calculated in the 'then' statement[/color]
)
« Last Edit: February 01, 2011, 07:48:53 PM by Lee Mac »

Lee Mac

  • Seagull
  • Posts: 12916
  • London, England
Re: If no input...
« Reply #2 on: February 01, 2011, 07:43:51 PM »
Better perhaps:

Code: [Select]
(setq PipeOD 25.0 PipeID 20.0)

(initget "Yes No")
(if (eq "Yes" (getkword (strcat "\nPipe ID: " (rtos PipeID) "mm \nChange Pipe ID? [Yes/No] <No> : ")))
  (progn
    (initget 7)
    (setq PipeID (getreal "\nEnter Pipe ID: "))
  )
)

(princ (strcat "\nPipe OD: " (rtos PipeOD) "mm, Pipe ID: " (rtos PipeID) "mm & " (rtos (/ (- PipeOD PipeID) 2.0)) "mm thk"))

MeasureUp

  • Bull Frog
  • Posts: 463
Re: If no input...
« Reply #3 on: February 01, 2011, 08:00:59 PM »
Thanks Lee.

Note that a null input to a getstring prompt returns an empty string ""

A few more things:

Code: [Select]
(setq[color=green] PipeOD 25.0[/color]) [color=green];; These are Reals, hence need to be converted to a string before used with 'strcat'[/color]
(setq [color=green]PipeID 20.0[/color])
...
Yes, I have read your 2nd reply.

Code: [Select]
(if (not TakeAction) ;; As above, this will never return T. Also, as you have used getstring, Yes or No will both return a non-nil value
...
With this one, how to make it return T?

Quote
(setq PipeID (getstring "Enter Pipe ID: ")) ;; Why are you prompting to Change the Pipe ID if the user has pressed enter for 'No'?
Sorry, It should be "Yes".

Lee Mac

  • Seagull
  • Posts: 12916
  • London, England
Re: If no input...
« Reply #4 on: February 01, 2011, 08:03:43 PM »
With this one, how to make it return T?

Use a string comparison test, i.e.:

Code: [Select]
(if (eq "" TakeAction)
  ...User Pressed Enter...
  ...User Entered Something...
)

Or perhaps

Code: [Select]
(if (eq "Yes" TakeAction)
  ...User Typed "Yes"
  ...User Typed something else, or pressed Enter...
)
 

But, I would use getkword in this situation, as shown in my example, since the use of getstring would mean you would have to also allow for case-sensitivity, i.e.

Code: [Select]
(if (eq "YES" (strcase TakeAction))
  ...User Typed "Yes"
  ...User Typed something else, or pressed Enter...
)
« Last Edit: February 01, 2011, 08:07:29 PM by Lee Mac »

MeasureUp

  • Bull Frog
  • Posts: 463
Re: If no input...
« Reply #5 on: February 01, 2011, 08:06:42 PM »
With this one, how to make it return T?

Use a string comparison test, i.e.:

Code: [Select]
(if (eq "" TakeAction)
  ...User Pressed Enter...
  ...User Entered Something...
)

But, I would use getkword in this situation, as shown in my example.

Thanks again.

Lee Mac

  • Seagull
  • Posts: 12916
  • London, England
Re: If no input...
« Reply #6 on: February 01, 2011, 08:07:58 PM »
Sorry, edited above...  :-P

MeasureUp

  • Bull Frog
  • Posts: 463
Re: If no input...
« Reply #7 on: February 01, 2011, 08:21:14 PM »
Sorry, edited above...  :-P
Thanks for your help, again.  :-)