Jump to content

Recommended Posts

Posted

Just experimenting with the 'while' function and looking for feedback as to the quality of my coding.

 

(defun c:test (/)
 (setq count 0)
 (setq 5dig (getint "\nEnter a 5 digit number: ")) ;_Enter a 5 digit number.
 (while
   (/= count 5)
    (setq 5dig (getint "\nTry again, please enter a 5 digit number: "))
    (setq strcount (itoa 5dig)) ;_Set a variable "strcount" to string from the interger from the variable "5dig"
    (setq count (strlen strcount) ;_Count the length of characters in the variable "strcount"
    )
 )
 (princ "\nYour are the Winner!")
 (princ)
)

 

Thanks

Posted

Or something like this

 
(defun 5int(/ num)
(while (not (and (or (initget 1)
      (setq num (getint "\nEnter a digits: ")))
  (if (minusp num)
    (eq 6 (strlen (itoa num)))
    (eq 5 (strlen (itoa num)))))
    )
 (princ "\nAllowed input 5 digits only..."))
     (if num num)
 )

Posted
looking for feedback as to the quality of my coding.

 

  • The variable count is initialised at zero rather than the number of digits of the integer specified by the user; hence the user will be asked to 'try again' upon correctly entering a 5-digit number:

Enter a 5 digit number: 12345
Try again, please enter a 5 digit number: 12345
Your are the Winner!

  • If the user enters a negative number, the program incorrectly validates the input:

Try again, please enter a 5 digit number: -1234
Your are the Winner!

  • If the user presses Enter at the prompt, the program will error since the itoa function cannot accept a null argument.

Try again, please enter a 5 digit number:
; error: bad argument type: fixnump: nil

  • The user has no way to optionally exit the loop without pressing Esc to force an error.

 

  • You have not declared your local variables (also known as variable localisation); to understand why this is important, see my tutorial here.

 

  • 'You're' or 'you are' is spelt incorrectly:

(princ "\n[color=red]Your are[/color] the Winner!")

I would suggest the following:

(defun c:test ( / int )
   (while
       (and
           (progn
               (initget 6)
               (setq int (getint "\nEnter a 5-digit number: "))
           )
           (/= 5 (strlen (itoa int)))
       )
       (princ "\nThat wasn't 5-digits.")
   )
   (if int (princ "\nYou are the winner!"))
   (princ)
)

Now:

 

  • User is not prompted again if 5-digits are entered the first time:

Enter a 5-digit number: 12345
You are the winner!

  • User cannot enter a negative number:

Enter a 5-digit number: -1234
Value must be positive and nonzero.

  • If the user presses Enter at the prompt, the program exits cleanly without error:

Command: test
Enter a 5-digit number:
Command:

  • As shown above, the user can optionally exit the program cleanly by pressing Enter at the prompt, without being stuck in the loop or the program resulting in an error.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...