Tutorials

AutoLISP Tutorial 8: Comparing Strings and User Feedback

Welcome to another insightful AutoLISP tutorial! Today, we’ll delve into the intricacies of string comparison and user feedback. As we work through our program, you’ll grasp how to interact with the user, understand their input, and guide them towards providing the desired response. With a straightforward structure and clear directives, this tutorial is tailor-made for a smooth learning experience.

Objective

Create a simple AutoLISP program that prompts the user to enter a specific word. If the user doesn’t input the word correctly, the program should give feedback suggesting that the “Cap Locks” might be on and prompt them to try again. The program ends by praising the user once they input the word correctly.

Instructions:

  1. Start by initializing a known string with the word “test”.
  2. Prompt the user to enter the word “test”.
  3. If the entered word doesn’t match the known string, display a message indicating that they might have “Cap Locks” on.
  4. Continue prompting the user to enter the word without “Cap Locks” until they get it right.
  5. Once the user inputs the correct word, display a congratulatory message.

Tip: Consider how you might use a loop to keep prompting the user until they get the answer right.


AutoLISP Tutorial 8 Solution: Comparing Strings and User Feedback

Introduction:

In this solution, we’ll be breaking down an AutoLISP program designed to interact with the user. The program’s primary objective is to prompt the user for specific input and provide feedback based on that input. It’s a simple yet effective way to understand string comparison, user feedback, and the application of loops in AutoLISP.

Complete Program:

(defun C:EX12-2 ( / str1 str2)
(setq str1 “test”)
(setq str2 (getstring “\nEnter the word test: “))
(while (/= str1 str2)
(princ “You must have Cap Locks on”)
(setq str2 (getstring “\nTurn off cap lock and enter the word test: “))
)
(princ “Well done”)
(princ)
)

Line-by-Line Explanation:

(defun C:EX12-2 ( / str1 str2)
This line defines a new function named C:EX12-2 with two local variables, str1 and str2.

(setq str1 “test”)
Here, we’re initializing the variable str1 with the string value “test”.

(setq str2 (getstring “\nEnter the word test: “))
This line prompts the user to input a string and stores it in the variable str2.

(while (/= str1 str2)
We initiate a while loop that will continue as long as the value of str1 is not equal to str2.

(princ “You must have Cap Locks on”)
If the inputted word doesn’t match “test”, this message is displayed, suggesting a potential reason.

(setq str2 (getstring “\nTurn off cap lock and enter the word test: “))
The user is prompted again to input the word, this time with a reminder to ensure “Cap Locks” is off.

(princ “Well done”)
Once the user inputs the correct word and exits the loop, they’re congratulated with this message.

(princ)
This final line just exits the function quietly.

By understanding this program, you’ve taken another step in mastering user interaction and control flow in AutoLISP. The techniques demonstrated here can be expanded upon for more complex applications and user interactions.


Conclusion:

And that wraps up our tutorial on string comparison and user interaction in AutoLISP. By now, you should have a better understanding of how to engage with users, provide feedback, and ensure they provide the information you’re looking for. Remember, the beauty of programming lies in the endless possibilities and adaptability of your code. Keep experimenting, keep refining, and most importantly, keep learning!