Reputation: 154
*Updated formatting of the code.
Good morning!
I'm teaching myself Common Lisp and I'm running into an issue where the program actually works, except if a certain character (",")is entered in as user input. The code (pretty short) is:
(defun welcome ()
(format t "Agent logged in. Status: Active, License to Kill: Active.~%~%"))
(defun getchoice3 ()
(let ((choice 1))
(format t "~%Enter your password: ")
(let ((response (read)))
(cond ((equal response '007)
(format t "~%~%Welcome James Bond, 007 ~%")(welcome))
(t (format t "~%~%Incorrect Response!~%~%") (getchoice3))))))
(getchoice3)
If I update that code to try to catch the error, like:
(defun welcome ()
(format t "Agent logged in. Status: Active, License to Kill: Active.~%~%"))
(defun getchoice3 ()
(let ((choice 1))
(format t "~%Enter your password: ")
(let ((response (read)))
(cond ((equal response '007)
(if (#'symbolp (response))
(getchoice3))
(format t "~%~%Welcome James Bond, 007 ~%")(welcome))
(t (format t "~%~%Incorrect Response!~%~%") (getchoice3))))))
(getchoice3)
It still works, and will go back to (getchoice3) on any symbol except ",". If I code it to look for the symbol explicitly:
(defun welcome ()
(format t "Agent logged in. Status: Active, License to Kill: Active.~%~%"))
(defun getchoice3 ()
(let ((choice 1))
(format t "~%Enter your password: ")
(let ((response (read)))
(cond ((equal response '007)
(if (/, (response))
(getchoice3))
(format t "~%~%Welcome James Bond, 007 ~%")(welcome))
(t (format t "~%~%Incorrect Response!~%~%") (getchoice3))))))
(getchoice3)
It still fails on the "," symbol and tells me that symbol doesn't have a backquote.
Ultimately I want the program to accept any letter or number, but no symbols and if it's entered, just ignore it and ask for input.
Any thoughts? Thank you!
Upvotes: 0
Views: 221
Reputation: 154
(This answer had a glaring error that was picked up by @ad absurdum - Thank you very much!)
(updated - rookie mistake has now been corrected - thank you again @ad-absurdum!)
I appreciate your assistance, especially @ad absurdum and @sds.
If anyone else needs to see the correct code, it's one change only, change (read) to (read-line), as they suggested:
(defun welcome ()
(format t "~% Status: Logged In Agent ID: 007 License to Kill:Active Messages from M: 0~%~%"))
(defun getchoice3 ()
(let ((choice 1))
(format t "~%Enter your password: ")
(let ((response (read-line)))
(cond ((string-equal response "007")
(format t "~%~%Welcome James Bond, 007 ~%")(welcome))
(t (format t "~%Incorrect Reponse.~%") (getchoice3))))))
(getchoice3)
Thank you again for your assistance!
Upvotes: 0
Reputation: 139321
Your code to read user input:
(format ...)
...
(read) ; input would be a Lisp data, using Lisp syntax
That should be something like this:
(format ...)
...
(finish-output ...) ; make sure output happens before waiting for input
(read-line ...) ; input would be text, returned as a string
Upvotes: 2