# Thread: Help with basic AUtoLISP calculator

1. Login to Give a bone

## Help with basic AUtoLISP calculator

Greetings! I'm learning LISP in an AutoCAD customization class in college. I wrote a calculator function with basic elements, and it works ok. Now I want to make it add more than 2 numbers, and I'm stuck. I can input more than the 2, but it only adds the first number and the last number input, not the total. I'm using "while" to make it loop, but I'm stuck. Can anyone help? Thanks in advance.

Laura

Here is the function:

Code:
```  (DEFUN C:CALC()
(SETQ NUM1(GETREAL "ENTER A NUMBER: "))

(SETQ OPER(getstring "\nenter math operator +,-,*,/"))

(SETQ NUM2(GETREAL "\nENTER NEXT NUMBER:"))

(COND
((= OPER "+")
(WHILE
(SETQ ANS (+ NUM1 NUM2))
(SETQ NUM1 (ANS))
(SETQ NUM2(GETREAL "\nENTER NEXT NUMBER:"))

)

))

((= OPER "-")

(SETQ ANS (- NUM1 NUM2))
)
((= OPER "*")

(SETQ ANS (* NUM1 NUM2))
)
((= OPER "/")

(SETQ ANS (/ NUM1 NUM2))
)
)
(PROMPT "THE ANSWER IS: " )(PRINC ANS)(PRINC)

)```

2. Login to Give a bone

## Re: Help with basic AUtoLISP calculator

Hi,

I have modified your codes a bit and I kept the codes simple just for learning.
Code:
```(DEFUN C:CALC (/ ANS NUM1 NUM2 OPER)
(SETQ NUM1 (GETREAL "\nENTER A NUMBER: "))
(SETQ OPER (getstring "\nenter math operator +,-,*,/"))
(SETQ NUM2 (GETREAL "\nENTER NEXT NUMBER:"))
(COND
((= OPER "+")
(SETQ ANS (+ NUM1 NUM2))
(WHILE (SETQ NUM2 (GETREAL "\nEnter Another number to sum :"))
(SETQ ANS (+ ANS NUM2))
)
)
((= OPER "-") (SETQ ANS (- NUM1 NUM2)))
((= OPER "*") (SETQ ANS (* NUM1 NUM2)))
((= OPER "/") (SETQ ANS (/ NUM1 NUM2)))
)
(if ans (PROMPT (strcat "\nTHE ANSWER IS: " (rtos ANS 2))))
(PRINC)
)```

3. Login to Give a bone

## Re: Help with basic AUtoLISP calculator

My version just type
'cal 2*3
6 will appear

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•